19

How to delete keys matching a certain pattern in redis using redis-cli. I would like to delete all foo's from the following list.

KEYS *

foo:1
foo:2
bar:1
foo:3
bar:2
foo:4
Rpj
  • 5,348
  • 16
  • 62
  • 122
  • 4
    There are multiple answered questions on the same subject, e.g http://stackoverflow.com/a/23399125/3160475 – Itamar Haber Feb 18 '15 at 03:58
  • possible duplicate of [How to atomically delete keys matching a pattern using Redis](http://stackoverflow.com/questions/4006324/how-to-atomically-delete-keys-matching-a-pattern-using-redis) – bitoiu Feb 18 '15 at 10:57
  • nice solution, even for large sets > 1000 keys. https://stackoverflow.com/questions/4006324/how-to-atomically-delete-keys-matching-a-pattern-using-redis#comment39607023_16974060 – Ajsti.pl - Maciej Szewczyk Dec 01 '17 at 15:43

4 Answers4

47

As mentioned in the comment on the question, there are many other answers to this here already. Definitely read the one linked above if you are thinking about doing this in a production sever.

The one I found most useful for occasional command-line cleanup was:

redis-cli KEYS "*" | xargs redis-cli DEL

from "How to atomically delete keys matching a pattern using Redis".

Community
  • 1
  • 1
Matt Plec
  • 594
  • 5
  • 3
5

I wanted to delete thousands of keys by pattern after some searches I found these points:

  • if you have more than one db on redis you should determine the database using -n [number]
  • if you have a few keys use del but if there are thousands or millions of keys it's better to use unlink because unlink is non-blocking while del is blocking, for more information visit this page unlink vs del
  • also keys are like del and is blocking

so I used this code to delete keys by pattern:

 redis-cli -n 2 --scan --pattern '[your pattern]' | xargs redis-cli -n 2 unlink 
mahdi yousefi
  • 807
  • 1
  • 9
  • 15
  • I am getting `(error) CROSSSLOT Keys in request don't hash to the same slot` I am using following to delete pattern `hi*` form cluster `redis-cli -u redis://localhost:6379 --scan --pattern 'hi*' | xargs redis-cli -u redis://localhost:6379 DEL` – roottraveller May 27 '20 at 05:25
  • hi @roottraveller I have no idea that where is the problem, but I searched a bit and found this link check if it works for you or not https://stackoverflow.com/questions/38042629/redis-cross-slot-error – mahdi yousefi May 30 '20 at 05:31
  • @roottraveller I had the same issue with redis cluster. One solution is to basically delete keys one by one, but using `redis-cli --pipe` to speed things up: https://stackoverflow.com/a/66179509/4256475 – Jason Hoetger Feb 12 '21 at 21:55
3

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

You can find the utility here:

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

Gabriel McAdams
  • 56,921
  • 12
  • 61
  • 77
2

If someone want to do same operation in AWS Elasticache redis, then you can connect with SSH to your EC2 server which is supposed to access AWS Redis server then you can use below command.

redis-cli -h <HOST> -p <PORT> --scan --pattern "patter*n" | xargs redis-cli -h <HOST> -p <PORT> unlink

Replace Host and port with AWS redis server host and port.

Also if your redis setup needs password authentication then use,

redis-cli -h <HOST> -p <PORT> -a <PASSWORD> --scan --pattern "patter*n" | xargs redis-cli -h <HOST> -p <PORT> -a <PASSWORD> unlink

Replace Host, port and password with AWS redis server host, port and password.

You can also use above commands for localhost.

Prem popatia
  • 321
  • 3
  • 14