4

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
hardy_sandy
  • 361
  • 4
  • 6
  • 13

4 Answers4

6

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

jaanus
  • 404
  • 5
  • 14
  • 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. – Andrew Oct 20 '20 at 18:28
  • Thanks! I've been searching around for a fix for my `xargs: unterminated quote` issue and this solved it. – Mike Jan 24 '21 at 17:34
4

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.

Ofir Luzon
  • 10,635
  • 3
  • 41
  • 52
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

h0x91B
  • 1,211
  • 15
  • 19
0

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.

Itamar Haber
  • 47,336
  • 7
  • 91
  • 117
  • 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 – hardy_sandy May 11 '17 at 08:36