Remove complete hashset at once in redis
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 ?
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.
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
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
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)