Last active
October 10, 2024 21:09
-
-
Save dk8996/1f8c92c4c5ea1b4e8997b80e826b90f4 to your computer and use it in GitHub Desktop.
Atomically delete keys matching a pattern in Redis
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
################################################################### | |
#Script Name : redis-delete | |
#Description : Given a host, port and pattern to delete from Redis. Example ./redis-delete.sh redis-XXXX.XXXX.com 6379 stage:* | |
#Args : <host> <port> <pattern> | |
#Author : Dimitry Kudryavtsev | |
#Email : [email protected] | |
################################################################### | |
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 ]\)'` | |
if [[ $reply = *[[:space:]]* ]] | |
then | |
keys=${reply#[0-9]*[[:space:]]} | |
for key in $keys; do | |
echo "Delete the following key: $key" | |
redis-cli -h $1 -p $2 DEL $key | |
done | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment