hardy_sandy
hardy_sandy

Reputation: 413

Delete keys from redis server using redis-cli

I am trying to delete the KEYS using pattern from redis server but it is not getting deleted.

Sample Keys

1) "flc_77sandeep-pant-back.int.dev.mykronos.com_personality:\xac\xed\x00\x05w\x03\t\xa0\x01"
2) "flc_77sandeep-pant-back.int.dev.mykronos.com_personality:\xac\xed\x00\x05w\x0e\x03\x01SecondaryKe\xf9:\xac\xed\x00\x05w\x03\t\x98\x02"
3) "flc_77sandeep-pant-back.int.dev.mykronos.com_personality:\xac\xed\x00\x05w\x03\t\xb8\x02"
4) "flc_77sandeep-pant-back.int.dev.mykronos.com_personality:\xac\xed\x00\x05w\x0e\x03\x01SecondaryKe\xf9:\xac\xed\x00\x05w\x02\t!"
5) "flc_1310sandeep-pant-back.int.dev.mykronos.com_personality:\xac\xed\x00\x05w\x0e\x03\x01SecondaryKe\xf9:\xac\xed\x00\x05w\x02\t~"
6) "flc_1310sandeep-pant-back.int.dev.mykronos.com_personality:\xac\xed\x00\x05w\x0e\x03\x01SecondaryKe\xf9:\xac\xed\x00\x05w\x03\t\xc0\x02"
7) "flc_-41sandeep-pant-back.int.dev.mykronos.com_personality:\xac\xed\x00\x05w\x03\t\xc5\x01"
8) "flc_77sandeep-pant-back.int.dev.mykronos.com_personality:\xac\xed\x00\x05w\x0e\x03\x01SecondaryKe\xf9:\xac\xed\x00\x05w\x03\t\x94\x03"
9) "flc_77sandeep-pant-back.int.dev.mykronos.com_personality:\xac\xed\x00\x05w\x03\t\xd3\x01"
10) "flc_77sandeep-pant-back.int.dev.mykronos.com_personality:\xac\xed\x00\x05w\x0e\x03\x01SecondaryKe\xf9:\xac\xed\x00\x05w\x03\t\xee\x02"

Command

redis-cli KEYS *sandeep-pant* | xargs redis-cli DEL

Output

xargs: WARNING: a NUL character occurred in the input.  It cannot be passed through in the argument list.  Did you mean to use the --null option?
xargs: unmatched double quote; by default quotes are special to xargs unless you use the -0 option
(integer) 0

Upvotes: 4

Views: 15255

Answers (5)

NicoXiang
NicoXiang

Reputation: 436

If your redis key is inserted by spring boot redis application, and use JdkSerializationRedisSerializer, keys will start with like \xac\xed\x00\x05w\x03, xargs can not read that key normally, in this scenario, you can use lua script.

  1. wirte lua script, saved as del.lua for example

    local key=KEYS[1]
    local list=redis.call("keys", key);
    for i,v in ipairs(list) do
        redis.call("del", v);
    end
    
  2. use redis-cli call this script

    redis-cli -h 127.0.0.1 -p 6379 --eval ./del.lua "*yourkeyspattern*" 
    

If you have performace concerned, try replace redis KEYS with SCAN command in lua script.

Upvotes: 0

jaanus
jaanus

Reputation: 423

If you don't want to write bash script use this one-liner

redis-cli --scan --pattern "*sandeep-pant*" | sed -e 's/^/"/g' -e 's/$/"/g' | xargs -i redis-cli del {}

Explanation:

  1. Gets the matched keys line by line
  2. sed adds quotes to the beginning and end of each key
  3. xargs deletes the records one by one.

{} is the marker where the key should be placed in the script

Upvotes: 6

Ofir Luzon
Ofir Luzon

Reputation: 10908

You should not use KEYS as it is a blocking operation, use SCAN instead. If you use glob pattern, surround it with quotes:

redis-cli --scan --pattern '*sandeep-pant*' | xargs -L 100 redis-cli del

You can use the -L 100 to batch the DEL ops with 100 keys each time.

Upvotes: 4

h0x91B
h0x91B

Reputation: 1246

Bash code:

for k in $(redis-cli -a password1 keys "*"); do
  echo "delete key '$k'";
  redis-cli -a password1 DEL $k;
done

Remove -a password1 if not need a password

Upvotes: 3

Itamar Haber
Itamar Haber

Reputation: 49942

You'd probably want to read the documentation about DEL - when you do, you'll notice that it does not accept key name patterns (i.e. wildcards), but requires the exact key names for deletion.

Upvotes: 0

Related Questions