Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A simple script to delete a bunch of keys from redis all at once (not to be used in production instances!)
#!/bin/bash
#
# A simple script to delete a bunch of keys from redis all at once.
#
# Don't use it in production!!!1!1!one
read -p "redis port to connect to? [6379] " redis_port
if [[ "${redis_port}" == "" ]]; then
redis_port="6379"
fi
if [[ ${redis_port} =~ ^[0-9]+$ ]]; then
while true; do
read -p "keys to delete (wildcard accepted, e.g. user:foo:bar:*): " pattern
# read all keys into a white space separated array
IFS=$'\r\n' GLOBIGNORE='*' :; matches=($(redis-cli --raw -p ${redis_port} keys "${pattern}"))
# list all matches
echo "Matching keys:"
matching_keys=0
for line in ${matches[@]}; do
echo $line
matching_keys=$((matching_keys+1))
done
if [[ "${matching_keys}" -eq 0 ]]; then
echo "No keys match your query, nothing to do."
else
read -p "Keys that will be deleted: ${matching_keys}. Really delete? [y/N] " confirm
case ${confirm} in
[yY]*)
echo -n "Keys deleted: "
redis-cli --raw -p ${redis_port} del "${matches[@]}"
;;
esac
fi
done
else
echo 'must specify a valid port number'
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment