database 在redis中,我如何删除密钥?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8799063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:25:19  来源:igfitidea点击:

In redis, how do i remove keys?

databaseredisredis-commands

提问by TIMEX

I want to remove keys that match "user*".

我想删除匹配“user*”的键。

how do I do that in redis command line?

我如何在 redis 命令行中做到这一点?

采纳答案by Donald Miner

This is not a feature right now to be able to do in one shot (see the comments in the DELdocumentation). Unfortunately, you are only left with using KEYS, looping through the results, and then using DELto remove each one.

这不是现在可以一次性完成的功能(请参阅DEL文档中的注释)。不幸的是,您只剩下 using KEYS,遍历结果,然后 usingDEL删除每个结果。

How about using bash a bit to help?

使用 bash 来帮助如何?

for key in `echo 'KEYS user*' | redis-cli | awk '{print }'`
 do echo DEL $key
done | redis-cli

To step through it:

要逐步完成:

  1. echo 'KEYS user*' | redis-cli | awk '{print $1}'-- get all the keys and strip out the extra text you don't want with awk.
  2. echo DEL $key-- for each one, create an echo statement to remove it.
  3. | redis-cli-- take the DEL statements and pass them back into the cli.
  1. echo 'KEYS user*' | redis-cli | awk '{print $1}'-- 使用 awk 获取所有键并删除您不想要的额外文本。
  2. echo DEL $key-- 对于每一个,创建一个 echo 语句来删除它。
  3. | redis-cli-- 获取 DEL 语句并将它们传递回 cli。

Not suggesting this is the best approach (you might have some issues if some of your usernames have spaces in them, but hopefully you get the point).

不建议这是最好的方法(如果您的某些用户名中有空格,您可能会遇到一些问题,但希望您明白这一点)。

回答by user1151446

Another compact one-liner I use to do what you want is:

我用来做你想做的另一个紧凑的单线是:

redis-cli KEYS "user*" | xargs redis-cli DEL

回答by Narendra Chamoli

Now there is a command to remove a key,i.e., DEL key [keys]

现在有一个删除密钥的命令,即 DEL key [keys]

DEL key...

DEL键...

回答by neilkimmett

Further to orangeoctopus' answer, you don't need the echoand pipe, you can pass commands as arguments into redis-cli. This means you can do

除了橙色章鱼的回答之外,您不需要echo和 管道,您可以将命令作为参数传递到redis-cli. 这意味着你可以做到

for key in `redis-cli "KEYS" "user*" | awk '{print }'`
 do redis-cli "DEL" "$key"
done

回答by make

Use this to remove redis keys having backslashes, quotes, double quotes or spaces:

使用它来删除具有反斜杠、引号、双引号或空格的 redis 键:

redis-cli KEYS "user*" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed "s/'/\\\\'/g" | sed 's/ /\\ /g' | xargs redis-cli DEL

redis-cli KEYS "user*" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed "s/'/\\\\'/g" | sed 's/ /\\ /g' | xargs redis-cli DEL

回答by jeetu

Using awk, find all matching keys from redis using redis-cli KEYScommand and pipe to redis-cli DELcommand.

使用awk,使用redis-cli KEYS命令和管道到redis-cli DEL命令从 redis 中查找所有匹配的键。

redis-cli KEYS "user*"  | awk '{ system("redis-cli DEL " ) }'

回答by Private

When using the oneliner, you can edit the pattern in case it escapes specific characters. For instance, to delete patterns like '\b test \b' use:

使用 oneliner 时,您可以编辑模式以防它转义特定字符。例如,要删除像 '\b test \b' 这样的模式,请使用:

redis-cli --raw KEYS '\b*' | sed 's/\b/\\b/g' | xargs redis-cli del

回答by Gabriel McAdams

I know this is old, but for those of you coming here form Google:

我知道这是旧的,但对于那些从谷歌来到这里的人:

I just published a command line interface utility to npm and github that allows you to delete keys that match a given pattern (even , or as you asked user) from a Redis database.

我刚刚向 npm 和 github 发布了一个命令行界面实用程序,它允许您从 Redis 数据库中删除与给定模式(甚至,或您询问的用户)匹配的键。

You can find the utility here:

您可以在此处找到该实用程序:

https://www.npmjs.com/package/redis-utils-cli

https://www.npmjs.com/package/redis-utils-cli