使用redis-cli从redis服务器删除密钥

时间:2017-05-10 09:03:34

标签: redis redis-cli

我正在尝试使用redis服务器中的模式删除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"

命令

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

输出

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

3 个答案:

答案 0 :(得分:2)

Bash代码:

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

如果不需要密码,请删除-a password1

答案 1 :(得分:1)

如果您不想编写bash脚本,请使用此单行

woff

说明:

  1. 逐行获取匹配的键
  2. sed在每个键的开头和结尾添加引号
  3. xargs逐个删除记录。
  4. {}是键应放在脚本中的标记

答案 2 :(得分:0)

您可能想要阅读documentation about DEL - 当您这样做时,您会注意到它不接受密钥名称模式(即通配符),但需要准确的密钥名称才能删除