- 
      
- 
        Save obscurerichard/f76302a0ad8ab9b20516 to your computer and use it in GitHub Desktop. 
    A bash script that scans Redis keys by pattern using SCAN
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/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 | 
what about redis-cli --scan --pattern '*'?
@farico much better! And easier 👍
#!/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
doneuse
./redis-scan.sh localhost 6378 0 *test*
Tried many modifications and it seems only scan is working and delete is not properly executed. After rescan the keys still existing
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
  
            
Small fix which worked for me: