0

Created a cron job that deletes specific keys in Redis.

Sample key: "\xac\xed\x00\x05t\x00\x15test"

Using bash and redis-cli it seems that keys with HEX values cannot be properly parsed and deleted:

Code below:


 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
Anonymous Duck
  • 2,942
  • 1
  • 12
  • 35

1 Answers1

1

Switched client to delete keys. Wrote a small python script

#!/usr/bin/env python3

import redis

redis_host = "localhost"
redis_port = 6379
redis_password = ""


def hello_redis():
    """Example Hello Redis Program"""
    print("Start of script")
    # step 3: create the Redis Connection object
    try:
        r = redis.StrictRedis(host=redis_host, port=redis_port, password=redis_password)
 
        for key in r.scan_iter("*key*"):
            print(key)
            r.delete(key)    
   
    except Exception as e:
        print(e)

    print("Terminating script")
if __name__ == '__main__':
    hello_redis()
Anonymous Duck
  • 2,942
  • 1
  • 12
  • 35