The HDEL
command in Redis is used to delete one or more fields from a hash stored at a key. It's commonly used when you need to remove specific data points from a large dataset stored in a Redis Hash.
Here's a simple example using the redis-py
library in Python:
import redis # Create a connection to the Redis server r = redis.Redis(host='localhost', port=6379, db=0) # Set a Redis Hash with multiple fields r.hmset('user:1000', {'name': 'John', 'age': '30', 'email': 'john@example.com'}) # Delete 'email' field from 'user:1000' r.hdel('user:1000', 'email') # Check remaining fields in 'user:1000' print(r.hgetall('user:1000')) # Output: {b'name': b'John', b'age': b'30'}
In this example, we first create a connection to the local Redis server. Then, a hash with key user:1000
is created with three fields - 'name', 'age', and 'email'. The hdel
command is used to delete the 'email' field from our hash. The remaining fields are then printed to the console.
None
response.1. What if the field does not exist in the hash?
If the specified field does not exist in the hash or the key does not exist, HDEL
will simply ignore it and return 0.
2. Can I delete multiple fields at once?
Yes, HDEL
supports deletion of multiple fields at once. Just pass the fields as additional arguments: r.hdel('user:1000', 'field1', 'field2', ...)
.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.