-
-
Save mstaack/39788d60491952dd549b94ef077a35b3 to your computer and use it in GitHub Desktop.
A bash script that deletes Redis keys by pattern using SCAN
This file contains hidden or 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