Created
April 20, 2014 22:27
-
-
Save itamarhaber/11126830 to your computer and use it in GitHub Desktop.
A bash script that deletes Redis keys by pattern using SCAN
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/bash | |
if [ $# -ne 3 ] | |
then | |
echo "Delete keys from Redis matching a pattern using SCAN & DEL" | |
echo "Usage: $0 <host> <port> <pattern>" | |
exit 1 | |
fi | |
cursor=-1 | |
keys="" | |
while [ $cursor -ne 0 ]; do | |
if [ $cursor -eq -1 ] | |
then | |
cursor=0 | |
fi | |
reply=`redis-cli -h $1 -p $2 SCAN $cursor MATCH $3` | |
cursor=`expr "$reply" : '\([0-9]*[0-9 ]\)'` | |
keys=${reply##[0-9]*[0-9 ]} | |
redis-cli -h $1 -p $2 DEL $keys | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Itamar,
You have a serious bug in line 21. It should be instead
keys=${reply/[0-9]+ /}
. Your code (wrongly) assumes that keys never have any digits in them.Best regards,
Dynamic Yield ltd.