85

I have a hash in Redis named "match/123/result".

I am adding entries to this hash using HSET and retrieving all entries at once using HGETALL.

I want to flush this hash, but there is no command like "HDELALL" (in redis-cli).

I am therefore using DEL to remove the hash name, like this:

DEL match/123/result

I could find only this approach to removing everything (hash and its contents) at once. Is there any other solution?

mirekphd
  • 4,799
  • 3
  • 38
  • 59
Pranav
  • 2,054
  • 4
  • 27
  • 34
  • 10
    No, and this is what you are expected to do. The empty value does not exist in Redis. An empty hash or no hash at all is the same for Redis. – Didier Spezia May 15 '13 at 12:20

6 Answers6

156

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.

Kurzyx
  • 372
  • 3
  • 14
Aman Garg
  • 3,122
  • 4
  • 24
  • 32
  • 1
    @AmanGarg thanks for your time but if you read the question carefully, I have already used DEL to flush hashset. I have asked for different/better solution than this. – Pranav Jun 28 '16 at 04:18
  • The doc says it removes keys. Is it actually removing the hash contents as well ? – Overdrivr Jan 22 '18 at 07:53
  • 2
    @Overdrivr. Yes, As I checked, it removes the complete hash set with that particular name. And as per the Doc if it removes keys then it means there will be no value against it which means the deletion of the complete hash set. – Aman Garg Jan 23 '18 at 06:34
4

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
Darren Hicks
  • 4,946
  • 1
  • 32
  • 35
  • I guess the question was how to delete entire set of elements (in hash, in set -> does not matter) with redis itself, meaning redis command. Suggestion you provided is just way to solve with programmatically looping. – Velaro Sep 06 '18 at 07:01
  • Thanks @Velaro for paying attention and contributing! Yes, this response may not be 100% on target, but neither are google search responses. I'm gonna leave this response intact for anyone who may serendipitously stumble upon this and have it contribute to the situation they find themselves in. – Darren Hicks Sep 28 '18 at 16:48
4

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

Basit Anwer
  • 6,742
  • 7
  • 45
  • 88
  • 1
    linear complexity is probably ok most of the time.. Both KEYS and DEL/HDEL has a linear complexity.. – Thomas Frössman Jun 01 '18 at 09:26
  • @ThomasFrössman yes you are right. If we have single key with string then time complexity O(N) and if we have multiple keys then time complexity O(N). If we want to delete multiple keys with list,sorted sets , sets and hashes then O(N) as well O(M). M is number of elements. – Muhammad Faizan Fareed Feb 28 '20 at 04:22
4

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)
dksr
  • 326
  • 2
  • 8
0

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);
});
            
Jitendra
  • 3,135
  • 2
  • 26
  • 42
0

We can delete all hashes at once. If we want to delete all sub hash of hash key 'dayz', the following code can do it.

import redis
r = redis.Redis()  
hkey = 'dayz'
r.hdel('dayz', *r.hkeys('dayz'))
Sons M
  • 1