Skip to content

Instantly share code, notes, and snippets.

@mstaack
Forked from itamarhaber/scan_del.sh
Created October 18, 2017 17:31
Show Gist options
  • Save mstaack/39788d60491952dd549b94ef077a35b3 to your computer and use it in GitHub Desktop.
Save mstaack/39788d60491952dd549b94ef077a35b3 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment