Remove complete hashset at once in redis
RedisRedis Problem Overview
I am having a hash set in redis names as = "match/123/result"
I am adding entries to set using "HSET" and retrieving all entries at a time using "HGETALL"
now, I want to flush this hash set, but there is no command like "HDELALL"
so I am using "DEL" to remove the hash set name itself, in this case I fire the command like this -
DEL match/123/result
Could find only this approach to remove everything at once. Is there any other solution ?
Redis Solutions
Solution 1 - Redis
If you want to delete or flush the 'myhash' hash.
Please use the command below:
redis-cli
redis> del myhash
Hope it will solve the problem.
Solution 2 - Redis
Here's a ruby-based way to remove all the keys in a Hash via a single, pipelined request:
def hdelall(key)
r = Redis.new
keys = r.hgetall(key).keys
r.pipelined do
keys.each do |k|
r.hdel key, k
end
end
end
Solution 3 - Redis
If you have a list of keys then you maybe can use hdel with multiple keys But i would certainly recommend not to use it since it has a complexity of O(N).
By default redis doesn't allow clear function inside a hashet so you'll have to use del
Solution 4 - Redis
This should work in Python (from "Redis in Action" book)
all_keys = list(conn.hgetall('some_hash_name').keys())
conn.hdel('some_hash_name', *all_keys)
Solution 5 - Redis
We can do it with one iteration: In my case, I have stored a hash in which key was my "field" and at value, I have stored an object. So you can change accordingly.
Object.keys(cartData).forEach((field)=>{
redisClient.hdel("YOUR KEY",field);
});