Deleting lots of keys in Redis, fast
5 March, 2021 by
Deleting lots of keys in Redis, fast
Administrator

 

Here’s a snippet that has helped me whenever I’ve wanted to delete a large number of keys in Redis. This snippet can be run from a bash (or equivalent) shell. In this example i want to delete all keys that begin with ‘sessions’.


redis-cli KEYS "sessions*" | xargs --max-args=100 --max-procs=8 redis-cli DEL

The first part of the script runs the KEYS command searching for keys that begin with ‘sessions’. (NOTE: It’s obviously useless to delete ALL keys this way. You might as well delete the .rdb or .aof file and restart the server.)

The second part runs the DEL command with a 100 arguments at a time. `–max-procs=8` means there are 8 processes running in parallel. Adjust –max-procs per the number of cores on your server.