1

Created a shell script that deletes keys that matches a pattern using the following script:

host=${1:-}
port=${2:-6379}
database=${3:-0}
pattern=${4:-"Test"}

cursor=-1
keys=""

echo "Starting to delete"
while [ $cursor -ne 0 ]; do
  if [ $cursor -eq -1 ]
  then
    cursor=0
  fi

  reply=`redis-cli -h $host -p $port SCAN $cursor MATCH $pattern`
  cursor=`expr "$reply" : '\([0-9]*[0-9 ]\)'`
  keys=${reply##[0-9]*[0-9 ]}
  echo "$keys"
  redis-cli -h $host -p $port DEL $keys
done

This is working fine for keys that doesn't have hex in keys. But when keys with hex characters involved, encountered an unknown operand error message. Sample key:

\xac\xed\x00\x05t\x00\x15test

Need suggestions on how to escape these characters.

The same problem here: https://stackoverflow.com/questions/46854691/how-to-delete-redis-keys-with-special-characters

AdminBee
  • 21,637
  • 21
  • 47
  • 71

1 Answers1

1

You should try to quote the keys

redis-cli -h $host -p $port DEL "$keys"

That should suffice. Without the quotes, the backslashes may be interpreted differently. It may also be necessary to double the backslashes. You could use sed for that.

redis-cli -h $host -p $port DEL `echo "$keys" | sed -e 's/\\/\\\\/'`

The shell is likely transforming "\x" into just "x".

Alexis Wilke
  • 2,697
  • 2
  • 19
  • 42