Dragonfly

Redis XDEL in Python (Detailed Guide w/ Code Examples)

Use Case(s)

The XDEL command in Redis is generally used for deleting a specific ID from a stream. This can be handy when you want to remove an erroneous or unneeded entry from your stream.

Code Examples

Here's how you can use the XDEL command using redis-py, which is the Redis client in Python.

import redis

r = redis.Redis(host='localhost', port=6379, db=0)
stream_key = 'mystream'
message_id = '0-1'

# Add message to stream
r.xadd(stream_key, {'field': 'value'})

# Delete message from stream
r.xdel(stream_key, message_id)

In this example, we first connect to the Redis server running on localhost at port 6379. Then, we specify the key of the stream ('mystream') and the ID of the message to delete ('0-1'). After adding a message to the stream, we then delete it with the xdel function.

Best Practices

Common Mistakes

FAQs

Q: Can I delete multiple IDs from a stream at once?

A: Yes, you can specify multiple IDs in the xdel command. Here's how:

r.xdel(stream_key, '0-1', '0-2', '0-3')

Was this content helpful?

Help us improve by giving us your feedback.

Similar Code Examples

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

System Design on AWS

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost