In redis, how do I remove keys?

I want to remove the keys matching "user *".

how to do it on the redis command line?

+45
database redis
Jan 10 '12 at 5:45
source share
7 answers

Now it is not a function to be able to do it in one shot (see comments in the DEL documentation ). Unfortunately, you only stay with KEYS , scrolling through the results, and then using DEL to delete each one.

How to use bash a bit to help?

 for key in `echo 'KEYS user*' | redis-cli | awk '{print $1}'` do echo DEL $key done | redis-cli 

To get through it:

  • echo 'KEYS user*' | redis-cli | awk '{print $1}' echo 'KEYS user*' | redis-cli | awk '{print $1}' - get all the keys and cross out the excess text that you don't want with awk.
  • echo DEL $key - create an echo statement for each of them to remove it.
  • | redis-cli | redis-cli - take DEL statements and pass them back to cli.

Do not indicate that this is the best approach (you may have some problems if some of your usernames have spaces in them, but hopefully you understand).

+31
Jan 10 2018-12-12T00:
source share

Another compact single line I use to do what you need:

 redis-cli KEYS "user*" | xargs redis-cli DEL 
+39
Nov 08 '12 at 15:09
source share

In addition to orangeoctopus answer you don't need echo and pipe, you can pass commands as arguments to redis-cli . That means you can do

 for key in `redis-cli "KEYS" "user*" | awk '{print $1}'` do redis-cli "DEL" "$key" done 
+2
Mar 16 '12 at 11:50
source share

Using awk , find all the relevant keys from redis using the redis-cli KEYS command redis-cli KEYS command and pipe to redis-cli DEL .

 redis-cli KEYS "user*" | awk '{ system("redis-cli DEL " $1) }' 
+1
Mar 18 '16 at 2:04 on
source share

I know this is out of date, but for those who came here, Google:

I just published a command line interface utility for npm and github that allows you to remove keys that match a given pattern (even if you asked the user) from the Redis database.

You can find the utility here:

https://www.npmjs.com/package/redis-utils-cli

0
Feb 24 '16 at 23:39
source share

When using oneliner, you can edit the template in case it eludes certain characters. For example, to remove patterns such as "\ b test \ b", use:

 redis-cli --raw KEYS '\\b*' | sed 's/\\b/\\\\b/g' | xargs redis-cli del 
0
Sep 09 '16 at 9:47
source share

Use this to remove backslash redis keys, quotes, double quotes, or spaces:

redis-cli KEYS "user*" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed "s/'/\\\\'/g" | sed 's/ /\\ /g' | xargs redis-cli DEL

0
Nov 25 '16 at 21:17
source share



All Articles