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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
[ "$#" -eq 1 ] || { echo "ERROR:1 argument required, $# provided"; exit 1; } | |
redis-cli KEYS "*$1*" | xargs redis-cli DEL |
./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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
[ "$#" -eq 1 ] || { echo "ERROR:1 argument required, $# provided"; exit 1; } | |
cursor=-1 | |
keys="" | |
while [ $cursor -ne 0]; do | |
if [ $cursor -eq -1 ] then | |
cursor=0 | |
fi | |
reply=`redis-cli SCAN $cursor MATCH $1` | |
cursor=`expr "$reply" : '\([0-9]*[0-9 ]\)'` | |
keys=${reply#[0-9]*[[:space:]]} | |
redis-cli DEL $keys | |
done |
Just note that `KEYS` can potentially run for a long time and consume a significant amount of RAM while Redis prepares the reply. An alternative and safer approach is using `SCAN` - see here for a sample script that uses it: http://stackoverflow.com/a/23399125/3160475
ReplyDeleteThanks for the info. Learnt a new thing. I have added an update above using SCAN.
DeleteNicely done :)
DeleteI need to add space and ; to rhe code when I used it
ReplyDeletewhile [ $cursor -ne 0 ]; do
if [ $cursor -eq -1 ]; then
cursor=0
fi