Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove complete hashset at once in redis

Tags:

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 ?

like image 945
Pranav Avatar asked May 15 '13 09:05

Pranav


People also ask

How do I clear Redis hash?

Redis HDEL command is used to remove the specified fields from the hash stored at a key and ignored the specified keys that do not exist within this hash. It is treated as an empty hash if the key does not exist, and this command returns 0.

How do I delete all Redis keys?

Redis Commands There are two major commands to delete the keys present in Redis: FLUSHDB and FLUSHALL. We can use the Redis CLI to execute these commands. The FLUSHDB command deletes the keys in a database. And the FLUSHALL command deletes all keys in all databases.

How do I delete multiple keys in Redis?

We can use keys command like below to delete all the keys which match the given patters “ user*" from the Redis. Note :- Not recommended on production Redis instance. I have written Lua script to delete multiple keys by pattern . Script uses the scan command to delete the Redis cache key in incremental iteration.

What is Hmset in Redis?

Redis HMSET command is used to set the specified fields to their respective values in the hash stored at the key. This command overwrites any existing fields in the hash. If the key does not exist, a new key holding a hash is created.


2 Answers

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.

like image 149
Aman Garg Avatar answered Sep 19 '22 21:09

Aman Garg


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 
like image 37
Darren Hicks Avatar answered Sep 20 '22 21:09

Darren Hicks