list 删除Redis列表中的所有条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9828160/
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
Delete all entries in a Redis list
提问by Paul A Jungwirth
Suppose you have a LIST datatype in Redis. How do you delete all its entries? I've tried this already:
假设您在 Redis 中有一个 LIST 数据类型。你如何删除它的所有条目?我已经试过了:
LTRIM key 0 0
LTRIM key -1 0
Both of those leave the first element. This will leave all the elements:
这两个都离开了第一个元素。这将留下所有元素:
LTRIM key 0 -1
I don't see a separate command to completely empty a list.
我没有看到一个单独的命令来完全清空列表。
回答by Anurag
Delete the key, and that will clear all items. Not having the list at all is similar to not having any items in it. Redis will not throw any exceptions when you try to access a non-existent key.
删除密钥,这将清除所有项目。根本没有列表类似于其中没有任何项目。当您尝试访问不存在的密钥时,Redis 不会抛出任何异常。
DEL key
Here's some console logs.
这是一些控制台日志。
redis> KEYS * (empty list or set) redis> LPUSH names John (integer) 1 redis> LPUSH names Mary (integer) 2 redis> LPUSH names Alice (integer) 3 redis> LLEN names (integer) 3 redis> LRANGE names 0 2 1) "Alice" 2) "Mary" 3) "John" redis> DEL names (integer) 1 redis> LLEN names (integer) 0 redis> LRANGE names 0 2 (empty list or set)
回答by ahmed hamdy
You can remove all values in the Listonly ifthe Listhas more than one value
您可以删除所有值列表只有在名单有多于一个值
LTRIM key -1 0
Example:
例子:
redis 127.0.0.1:6379> RPUSH mylist four 1 3 1
(integer) 4
redis 127.0.0.1:6379> KEYS *
1) "newKey"
2) "firstHash"
3) "secondList"
4) "test4"
5) "firstList"
6) "mylist"
redis 127.0.0.1:6379> LTRIM mylist -1 0
OK
redis 127.0.0.1:6379> LRANGE mylist 0 -1
(empty list or set)
redis 127.0.0.1:6379> KEYS *
1) "newKey"
2) "firstHash"
3) "secondList"
4) "test4"
5) "firstList"
Butif the List has one value only , you must use
但如果列表只有一个值,则必须使用
DEL key
回答by Mohd Abdul Mujib
Just use LTRIM. ...Aaaand the magichere is to use start
greater thanend
.
只需使用LTRIM。...Aaaand这里的魔法是使用start
大于end
。
LTRIM key 99 0
And...boom! there goes the whole list of elements. Just * pooof * right in front of your eyes!! No residue remaining, and absolutely no questions asked.
还有……轰!有整个元素列表。就在你眼前*噗*!没有残留,绝对没有问题。
Note:This will cause the key to be "removed"(as in deleted) as described here
注意:这将导致键被“删除”(如删除)所描述的在这里
...if start is larger than the end of the list, or start > end, the result will be an empty list (which causes key to be removed).
...如果 start 大于列表的末尾,或者 start > end,结果将是一个空列表(这会导致键被删除)。
回答by The-null-Pointer-
This might be a late response but am sticking it here just in case someone still needs that functionality.
这可能是一个迟到的响应,但我坚持在这里以防万一有人仍然需要该功能。
Short answer
简答
ltrim mylist 0 - (n+1) where mylist is key and n is length of mylist.
ltrim mylist 0 - (n+1) 其中 mylist 是键,n 是 mylist 的长度。
Long answer
长答案
The way ltrim works is that, it takes two indices and return the elements that fall between them inclusive of the indices.
ltrim 的工作方式是,它需要两个索引并返回它们之间的元素,包括索引。
Ltrim list startIndex endIndex
Ltrim 列表 startIndex endIndex
Example assuming we have a redis list with key mylist containing 10 entries:
示例假设我们有一个包含 10 个条目的密钥 mylist 的 redis 列表:
ltrim mylist 0 5 will trim the list to elements starting from index 0 through to index 5. And discard those that fall outside that range.
ltrim mylist 0 5 将列表修剪为从索引 0 开始到索引 5 的元素。并丢弃那些不在该范围内的元素。
Fortunately redis list operations support negative indexing which proves extremely useful in some situations. Typically when you don't know the length of the list.
幸运的是 redis 列表操作支持负索引,这在某些情况下非常有用。通常当您不知道列表的长度时。
-1 refers to last element, - 2 penultimate element, etc. And (-n) is the first element.
-1 是指最后一个元素,-2 是倒数第二个元素,等等。 (-n) 是第一个元素。
Out of range indexes are not harmful. If the end index is greater than length of the list, redis treats it as equal to last index.
超出范围的索引没有害处。如果结束索引大于列表的长度,redis 将其视为等于最后一个索引。
This is why ltrim mylist 0, -(n +1) clear the list. It does so because (-n) is equivalent to index 0. Adding 1 to it leaves no element within that range since that will be before the first element.
这就是为什么 ltrim mylist 0, -(n +1) 清除列表。这样做是因为 (-n) 等价于索引 0。向它添加 1 不会留下该范围内的元素,因为它将在第一个元素之前。
回答by idioto
I tried this and it works for me. Just change myListfor your list name and execute it
redis-cli KEYS "myList:*" | xargs redis-cli DEL
我试过这个,它对我有用。只需将myList更改为您的列表名称并执行它
redis-cli KEYS "myList:*" | xargs redis-cli DEL
回答by deimosaffair
the accepted answer is wrong. suppose i have
接受的答案是错误的。假设我有
redis 127.0.0.1:6379> KEYS *
1) "newKey"
2) "firstHash"
3) "secondList"
4) "test4"
5) "firstList"
6) "mylist"
to use ahmed's example, which is actually correct. now, if i do:
使用艾哈迈德的例子,这实际上是正确的。现在,如果我这样做:
DEL 'test4'
i end up with:
我最终得到:
1) "newKey"
2) "firstHash"
3) "secondList"
4) "firstList"
5) "mylist"`
so, i did not remove all entries from the 'test4' list, i removedtest4 itself. not the same thing. not at all. i have a little app where the list keys are hashes computed from several data(well, aren't they all?), those lists sometimes are cleared, but the semantics of a empty list and a non-existent list are very different. so, no, i do not want to DEL 'myhashes', i want just to remove all entries.
所以,我没有从“TEST4”列表中删除所有的条目,我删除TEST4本身。不是一回事。一点也不。我有一个小应用程序,其中列表键是从几个数据计算出来的哈希值(好吧,不是全部吗?),这些列表有时会被清除,但空列表和不存在列表的语义是非常不同的。所以,不,我不想删除'myhashes',我只想删除所有条目。
beware, oh ye who wanders here.
当心,哦,你们在这里徘徊。