Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 983
hardy_sandy Avatar asked Dec 23 '22 18:12

hardy_sandy


1 Answers

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

like image 118
jaanus Avatar answered Jan 27 '23 22:01

jaanus