How do I remove keys?
Asked Answered
J

11

110

I want to remove keys that match "user*".

How do I do that in redis command line?

Jitter answered 10/1, 2012 at 5:45 Comment(1)
in redis you don't remove keys but keys remove themselves.Lucent
C
61

This is not a feature right now to be able to do in one shot (see the comments in the DEL documentation). Unfortunately, you are only left with using KEYS, looping through the results, and then using DEL to remove each one.

How about using bash a bit to help?

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

To step through it:

  1. echo 'KEYS user*' | redis-cli | awk '{print $1}' -- get all the keys and strip out the extra text you don't want with awk.
  2. echo DEL $key -- for each one, create an echo statement to remove it.
  3. | redis-cli -- take the DEL statements and pass them back into the cli.

Not suggesting this is the best approach (you might have some issues if some of your usernames have spaces in them, but hopefully you get the point).

Chenoweth answered 10/1, 2012 at 5:57 Comment(0)
G
109

Another compact one-liner I use to do what you want is:

redis-cli KEYS "user*" | xargs redis-cli DEL
Gerhan answered 8/11, 2012 at 15:9 Comment(6)
This is great, thanks. It is sad that Redis doesn't have this functionality natively.Hetman
If you have multible databases (keyspaces) then this is the trick: Lets say you need to delete keys in db3: redis-cli -n 3 KEYS "prefix:*" | xargs redis-cli -n 3 DELGadroon
is there a one liner if you need to provide AUTH (i.e. login) prior to executing the delete command?Atlanta
'redis-cli help' says: -a <password> Password to use when connecting to the server. Hope it helps.Gerhan
I had problems with this one. In case you have many entries to delete is better to use the awk based ones below.Byre
Getting this: (error) ERR wrong number of arguments for 'keys' commandOmland
C
61

This is not a feature right now to be able to do in one shot (see the comments in the DEL documentation). Unfortunately, you are only left with using KEYS, looping through the results, and then using DEL to remove each one.

How about using bash a bit to help?

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

To step through it:

  1. echo 'KEYS user*' | redis-cli | awk '{print $1}' -- get all the keys and strip out the extra text you don't want with awk.
  2. echo DEL $key -- for each one, create an echo statement to remove it.
  3. | redis-cli -- take the DEL statements and pass them back into the cli.

Not suggesting this is the best approach (you might have some issues if some of your usernames have spaces in them, but hopefully you get the point).

Chenoweth answered 10/1, 2012 at 5:57 Comment(0)
A
23

Now there is a command to remove a key,i.e., DEL key [keys]

DEL key...

Archivist answered 23/4, 2018 at 17:31 Comment(0)
R
6

Further to orangeoctopus' answer, you don't need the echo and pipe, you can pass commands as arguments into redis-cli. This means you can do

for key in `redis-cli "KEYS" "user*" | awk '{print $1}'`
 do redis-cli "DEL" "$key"
done
Rarefaction answered 16/3, 2012 at 11:50 Comment(0)
A
6

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

redis-cli KEYS "user*"  | awk '{ system("redis-cli DEL " $1) }'
Ahvenanmaa answered 18/3, 2016 at 14:4 Comment(0)
M
2

In order to delete all the redis keys of db 3:

redis-cli -n 3 --scan | xargs redis-cli -n 3 DEL
Meryl answered 23/6, 2021 at 10:37 Comment(0)
O
2

If there are multiple keys in a pattern, for example : user1, user2, user3. To delete all keys which satisfy a pattern, use the below syntax.

redis-cli -c --scan --pattern '*user*' | xargs -l -r redis-cli -c del

With this command, it will scan and finds all the keys which matches the above pattern and passes this to xargs which deletes the keys one by one.

Note the use of -l arguments to delete keys one by one and -r to execute the delete command only if there is any input to the delete command.

Orthopedic answered 22/2, 2023 at 9:2 Comment(0)
E
1

Use this to remove redis keys having backslashes, 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

Esma answered 25/11, 2016 at 21:17 Comment(0)
D
0

I know this is old, but for those of you coming here form Google:

I just published a command line interface utility to npm and github that allows you to delete keys that match a given pattern (even , or as you asked user) from a Redis database.

You can find the utility here:

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

Deforce answered 24/2, 2016 at 23:39 Comment(0)
Y
0

When using the oneliner, you can edit the pattern in case it escapes specific characters. For instance, to delete patterns like '\b test \b' use:

redis-cli --raw KEYS '\\b*' | sed 's/\\b/\\\\b/g' | xargs redis-cli del
Yacketyyak answered 9/9, 2016 at 9:47 Comment(0)
B
0

On Windows, simply type

DEL KEYS nameofyourkey
Baggs answered 15/9, 2022 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.