Skip to content

Instantly share code, notes, and snippets.

@obscurerichard
Forked from itamarhaber/scan_del.sh
Last active March 13, 2023 09:17
Show Gist options
  • Save obscurerichard/f76302a0ad8ab9b20516 to your computer and use it in GitHub Desktop.
Save obscurerichard/f76302a0ad8ab9b20516 to your computer and use it in GitHub Desktop.
A bash script that scans Redis keys by pattern using SCAN
#!/usr/bin/env bash
# redis-scan.sh
#
# Adapted by @obscurerichard from itamarhaber/scan_del.sh:
# https://gist.github.com/itamarhaber/11126830
#
# Thanks @czerasz and @tenlee2012 for fixes
#
# Usage:
# ./redis-scan.sh localhost 6378 0 '*test*'
#
# NOTE: if your redis-cli supports '--scan' use that instead, thanks @ferico
#
# redis-cli --scan --pattern '*'
#
if [ "$#" -lt 2 ]
then
echo "Scan keys in Redis matching a pattern using SCAN (safe version of KEYS)"
echo "Usage: $0 <host> [port] [database] [pattern]"
exit 1
fi
host=${1:-}
port=${2:-6379}
database=${3:-0}
pattern=${4:-\*}
cursor=-1
keys=""
echo "host=${host},port=${port},database=${database}"
while [[ "$cursor" -ne 0 ]]; do
if [[ "$cursor" -eq -1 ]]
then
cursor=0
fi
reply=$(redis-cli -h "$host" -p "$port" -n "$database" SCAN "$cursor" MATCH "$pattern")
cursor=$(expr "$reply" : '\([0-9]*[0-9 ]\)')
keys=${reply//$cursor/}
if [ -n "$keys" ]; then
echo "$keys"
fi
done
@czerasz
Copy link

czerasz commented Dec 14, 2016

Small fix which worked for me:

host=${1:-}
port=${2:-6379} # This line was changed
database=${3:-0}
pattern=${4:-\*} # This line was changed

@farico
Copy link

farico commented Mar 28, 2017

what about redis-cli --scan --pattern '*'?

@czerasz
Copy link

czerasz commented Jun 4, 2017

@farico much better! And easier 👍

@tenlee2012
Copy link

tenlee2012 commented Jan 7, 2020

#!/bin/bash

if [ "$#" -lt 2 ]
then
  echo "Scan keys in Redis matching a pattern using SCAN (safe version of KEYS)"
  echo "Usage: $0 <host> [port] [database] [pattern]"
  exit 1
fi
host=${1:-}
port=${2:-6379}
database=${3:-0}
pattern=${4:-\*}
cursor=-1
keys=""

echo "host=${host},port=${port},database=${database}"
while [[ "$cursor" -ne 0 ]]; do
  if [[ "$cursor" -eq -1 ]]
  then
    cursor=0
  fi

  reply=$(redis-cli -h "$host" -p "$port" -n "$database" SCAN "$cursor" MATCH "$pattern")
  cursor=$(expr "$reply" : '\([0-9]*[0-9 ]\)')
  #keys=${reply##[0-9]*[0-9 ]}
  keys=${reply//$cursor/}
  if [ -n "$keys" ]; then
    echo $keys
  fi
  #echo $keys
done

use

./redis-scan.sh localhost 6378 0 *test*

@darkmakukudo
Copy link

Tried many modifications and it seems only scan is working and delete is not properly executed. After rescan the keys still existing

@ilaotan
Copy link

ilaotan commented Feb 2, 2023

Tried many modifications and it seems only scan is working and delete is not properly executed. After rescan the keys still existing

redis-cli -h xxxxxx -a 'xxxxxx' -n 0 -p 6379 --scan --pattern 'xxxxxx*' | xargs -L 2000 redis-cli -h xxxxxx -a 'xxxxxx' -n 0 -p 6379 del

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment