How to remove keys matching a specific pattern in redis

How to remove keys matching a specific pattern in redis using redis-cli. I would like to remove all foo from the following list.

KEYS * foo:1 foo:2 bar:1 foo:3 bar:2 foo:4 
+9
source share
3 answers

As mentioned in the commentary on this question, there are already many other answers. Definitely read the link above if you are thinking about this in production.

The one I found most useful for randomly cleaning the command line was:

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

from "How to atomize keys matching a pattern using Redis . "

+23
source

I just published a command line interface utility for npm and github that allows you to remove 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

+3
source

I wanted to remove thousands of keys from the template, after some searches I found the following items:

  • if you have more than 1dB on Redis, you must define the database using -n [number]
  • if you have several keys, use del but if there are thousands or millions of keys, it is better to use unlink because unlink is non-blocking and del is blocking, for more information visit this page unlink vs del
  • also keys as del and locks

so I used this code to remove keys by pattern:

  redis-cli -n 2 --scan --pattern '[your pattern]' | xargs redis-cli -n 2 unlink 
0
source

Source: https://habr.com/ru/post/1213584/


All Articles