How to delete keys matching a certain pattern in redis

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 

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".


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