Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A bash script that deletes Redis keys by pattern using SCAN
#!/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
@OmriManor
Copy link

OmriManor commented May 18, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment