Say I have millions of prefix:<numeric_id>
keys.
I want to purge them all atomically.
How to atomically delete keys matching a pattern using Redis shows many options. Some use redis-cli
or Bash script but I need to do it using my client, programmatically.
A Lua script approach is promising, but solutions with KEYS
command fail with “too many elements to unpack” error.
How to achieve this?
2
Answers
The following Lua script uses
SCAN
command, so it deletes in chunks within the script - avoiding the "too many elements to unpack" error.It returns how many times
SCAN
was called and how many keys were deleted.Use as:
Note it will block the server while running, so it is not advised for production as is.
For production, consider changing
DEL
forUNLINK
. You can also return the cursor (instead of repeating inside the script until it is zero) and add COUNT parameter to SCAN to throttle (see Is there any recommended value of COUNT for SCAN / HSCAN command in REDIS?). This way you do it in chunks instead of one go, similar to How can I get all of the sets in redis?Or you can do something more sophisticated using the approach stated in this answer: Redis `SCAN`: how to maintain a balance between newcomming keys that might match and ensure eventual result in a reasonable time?
Lua is a great option as long as you are not using Redis Cluster or all the keys you want to delete are on the same shard.
If you need to delete keys from multi-shards you can still use Lua but you’ll have to send the
Eval
command to all the shards “manually”.An alternative that does it for you, is using RedisGears (a Redis module), which allow you to write a cross cluster del command based on some criteria.
See example: https://oss.redislabs.com/redisgears/examples.html#delete-by-key-prefix