Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete keys matching a pattern in Redis Cluster

I have tried method in this question, but it does not work since I'm working in cluster mode, and redis told me:

(error) CROSSSLOT Keys in request don't hash to the same slot

like image 304
xingbin Avatar asked Dec 11 '18 01:12

xingbin


People also ask

How do I delete all keys matching a pattern in Redis?

Redis does not offer a way to bulk delete keys. You can however use redis-cli and a little bit of command line magic to bulk delete keys without blocking redis. This command will delete all keys matching users:* If you are in redis 4.0 or above, you can use the unlink command instead to delete keys in the background.

How do I clear a key in Redis?

Redis Commands There are two major commands to delete the keys present in Redis: FLUSHDB and FLUSHALL. We can use the Redis CLI to execute these commands. The FLUSHDB command deletes the keys in a database. And the FLUSHALL command deletes all keys in all databases.

How do I delete multiple keys in Redis?

We can use keys command like below to delete all the keys which match the given patters “ user*" from the Redis. Note :- Not recommended on production Redis instance. I have written Lua script to delete multiple keys by pattern . Script uses the scan command to delete the Redis cache key in incremental iteration.

Which command is used to delete the key in Redis?

Redis - Keys Del Command Redis DEL command is used to delete the existing key in Redis.


2 Answers

Answers for that question try to remove multiple keys in a single DEL. However, keys matching the given pattern might NOT locate in the same slot, and Redis Cluster DOES NOT support multiple-key command if these keys don't belong to the same slot. That's why you get the error message.

In order to fix this problem, you need to DEL these keys one-by-one:

redis-cli --scan --pattern "foo*" |xargs -L 1 redis-cli del

The -L option for xargs command specifies the number of keys to delete. You need to specify this option as 1.

In order to remove all keys matching the pattern, you also need to run the above command for every master nodes in your cluster.

NOTE

  1. With this command, you have to delete these keys one-by-one, and that might be very slow. You need to consider re-designing your database, and use hash-tags to make keys matching the pattern belong to the same slot. So that you can remove these keys in a single DEL.

  2. Either SCAN or KEYS command are inefficient, especially, KEYS should not be used in production. You need to consider building an index for these keys.

like image 184
for_stack Avatar answered Oct 19 '22 18:10

for_stack


Building on for_stack's answer, you can speed up mass deletion quite a bit using redis-cli --pipe, and reduce the performance impact with UNLINK instead of DEL if you're using redis 4 or higher.

redis-cli --scan --pattern "foo*" | xargs -L 1 echo UNLINK | redis-cli --pipe

Output will look something like this:

All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 107003

You do still need to run this against every master node in your cluster. If you have a large number of nodes, it's probably possible to automate the process further by parsing the output of CLUSTER NODES.

like image 20
Jason Hoetger Avatar answered Oct 19 '22 19:10

Jason Hoetger