Skip to content

Instantly share code, notes, and snippets.

@itamarhaber
Created April 20, 2014 22:27
Show Gist options
  • Save itamarhaber/11126830 to your computer and use it in GitHub Desktop.
Save itamarhaber/11126830 to your computer and use it in GitHub Desktop.
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

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