Skip to content

Instantly share code, notes, and snippets.

@selfdeceited
Forked from jdp/redis-delkeys.sh
Last active November 29, 2023 16:46
Show Gist options
  • Save selfdeceited/ca7568f9c378f48be10a9de5231dbc50 to your computer and use it in GitHub Desktop.
Save selfdeceited/ca7568f9c378f48be10a9de5231dbc50 to your computer and use it in GitHub Desktop.
Delete keys matching a pattern from Redis (exe Lua script; working on Windows clients)
#!/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 EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 "$PATTERN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment