Deleting all non-expiring keys in a Redis sever from command line

23 Dec

This ending year, we have been reading, installing and testing several in RAM key-value storage(NoSQL), engines.   We have decided to give Redis a try mainly because it can save the data to disk, in case you have to restart a server or it crashes, you have have your data back.  Our experience with Redis has been mixed, good and bad things, but that will be for another post…

One of the main disadvantages of newer, less mature storage engines than for example MySQL, is the administration tools and features.  In this case, as data is kept into memory, if you create a key-value pair, without setting the expiry time, it will stay there in aeternum.  Specially on Redis, since data is recovered from disk when the service starts.   This creates an administrative problem when you cannot control how programmers use Redis; unfortunately there is no simple way of retrieving all non-expiring keys or keys with more than n seconds to expire from the server in order to delete them.  We have already requested this feature: But in the mean time, we came out with a one line set of commands to delete them:

redis-cli keys  "*" | while read LINE ; do TTL=`redis-cli ttl $LINE`; if [ $TTL -eq -1 ]; then echo "Del $LINE"; RES=`redis-cli del $LINE`; fi; done;

You still need to retrieve all keys, then ask for their expiry time, but is not too slow.  You can also modify it to delete keys expiring in more than n seconds, for that case you can use:

redis-cli keys  "*" | while read LINE ; do TTL=`redis-cli ttl $LINE`; if [ $TTL -ge  3600 ]; then echo "Del $LINE"; RES=`redis-cli del $LINE`; fi; done;

Where 3600 is the min expiry time you want to delete in seconds.

If you only want to delete keys matching a pattern you can edit the “*” part and add a wildcard expression.

You can probably improve the commands, but this helped us delete the necessary keys, hope it can help you too!

Switch to our mobile site