Tech Recipe : Deleting lots of keys in Redis, fast

I haven’t posted anything in here for a while, so i thought i’d post a short snippet until i can think of something more meaty. 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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s