Skip to content

Instantly share code, notes, and snippets.

@2bj
Forked from jdp/redis-delkeys.sh
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2bj/81320c1d549cb86b6b09 to your computer and use it in GitHub Desktop.
Save 2bj/81320c1d549cb86b6b09 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Usage: ./redis-delkeys.sh [-h host] [-p port] [-n db] pattern
#
# Matches keys with the KEYS command matching pattern
# and deletes them from the specified Redis DB.
set -e
HOST="localhost"
PORT="6379"
DB="0"
while getopts "h:p:n:" opt; do
case $opt in
h) HOST=$OPTARG;;
p) PORT=$OPTARG;;
n) DB=$OPTARG;;
\?) echo "invalid option: -$OPTARG" >&2; exit 1;;
esac
done
shift $(( $OPTIND -1 ))
PATTERN="$@"
if [ -z "$PATTERN" ]; then
echo "pattern required" >&2
exit 2
fi
redis-cli -h $HOST -p $PORT -n $DB --raw keys $PATTERN |
xargs redis-cli -h $HOST -p $PORT -n $DB del
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment