Monday, January 26, 2015

Shell script to delete redis keys matching a pattern


A quick way to delete redis keys from the command line would be:
        redis-cli DEL "del_key"

If you wish to delete all keys matching a certain pattern,
        redis-cli KEYS "*del_pattern*" | xargs redis-cli DEL

A more easy shell script for it would be:

Save it as redis-delete.sh and you can just run it as:
        ./redis-delete.sh del_pattern

UPDATE:
The above code using KEYS does not scale well and so for a more performance efficient code, we can use SCAN which is a cursor-based iterator.