Delete keys from redis server using redis-cli

12,592

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
12,592

Related videos on Youtube

hardy_sandy
Author by

hardy_sandy

Updated on May 25, 2022

Comments

  • hardy_sandy
    hardy_sandy almost 2 years

    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
    
    • h0x91B
      h0x91B almost 7 years
      * is a bash control character
  • hardy_sandy
    hardy_sandy almost 7 years
    I tried deleting keys using pattern and it is working. [root@dmc31 ~]# redis-cli SET key1 "Hello" OK [root@dmc31 ~]# redis-cli SET key2 "World" OK [root@dmc31 ~]# redis-cli KEYS *key* 1) "key2" 2) "key1" [root@dmc31 ~]# redis-cli KEYS *key* | xargs redis-cli DEL (integer) 2
  • h0x91B
    h0x91B almost 7 years
    Yeah, this one much better then mine.
  • Andrew
    Andrew over 3 years
    Ah thanks, resolved my issue. This used to work fine without the sed bits but stopped working on some keys even though they don't appear to have any strange quotes/encoding.
  • Mike
    Mike over 3 years
    Thanks! I've been searching around for a fix for my xargs: unterminated quote issue and this solved it.