How to Set Expiry to Multiple Keys in Redis

Often, developers forget to set an expiry on keys, and redis begins to run out of memory. While you can set an expiry to newly generated keys, you still need a way to set an expiry to keys already sitting in memory.

This command will set expiry to keys matching users:*

How does the Script Work?

  1. First, we use redis-cli --scan --pattern <pattern> to get a list of keys, one key per line.
  2. We then run an awk script for each key. This awk script is a little complex, so we will break it down
  3. The awk script has a command template expire __key__ 1000. This is the command that will run for all keys.
  4. For each matching key, the awk command replaces __key__ with the actual key
  5. Next, we convert the string command to redis protocol format. This is the raw format that redis understands.
  6. Finally, we pipe the raw commands to redis-cli using the –pipe flag. This is the most efficient way to send bulk commands to redis.

Customizing this Script

  1. You can customize cmd_template to run any single key redis command against multiple keys matching a pattern.
  2. Redis scan allows glob style patterns only, regex is not supported. So users:* will match users:123 as well as users:123:favourites. You can put in a grep in the pipeline after the first redis-cli and before the awk script.
  3. If your redis server is not on localhost, you will have to provide the host, port and password twice in the command, basically whenever you call redis-cli.

See Also