删除 Redis 列表中的所有条目

假设你在 Redis 有一个 LIST 数据类型,你如何删除它的所有条目呢:

LTRIM key 0 0
LTRIM key -1 0

这两个元素都留下了第一个元素,这将留下所有的元素:

LTRIM key 0 -1

我没有看到完全清空列表的单独命令。

82301 次浏览

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.

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)

UPDATE 06-11-2021

There Different ways to Remove all element from the List :

Step 1: Using General DEL Command for delete any key into Redis like Anurag's solution

DEL list

Step 2: Using LTRIM Command and Applying The next conditional from Documentation

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).

SO any of next Commands will works or Mohd Abdul Mujib's solution

LTRIM list 999 0


LTRIM list 1 0


LTRIM list 4 1

But Take care about using negative numbers as The start index, as From Documentation

start and end can also be negative numbers indicating offsets from the end of the list, where -1 is the last element of the list, -2 the penultimate element and so on.

Next Command Will Remove all elements under list IF list include more than one elements BUT IF list include only one element will Not Remove any thing

LTRIM list -1 0

Explain

First Case (list include more than one elements) the index -1 as the start will translate to the last element which has 4 index(if list include 4 elements) SO the condition start > end has been applyed

Second Case (list include one elements) the index -1 as the start will translate to the last element which has 0 index SO the condition become start == end Not start > end

Here Example for Above commands:

redis 127.0.0.1:6379> RPUSH mylist four 1 3 1
(integer) 4
redis 127.0.0.1:6379> KEYS *
1) "test4"
2) "firstList"
3) "mylist"
redis 127.0.0.1:6379> LTRIM mylist 999 0
OK
redis 127.0.0.1:6379> KEYS *
1) "test4"
2) "firstList"
redis 127.0.0.1:6379> RPUSH mylist four 1 3 1
(integer) 4
redis 127.0.0.1:6379> KEYS *
1) "test4"
2) "firstList"
3) "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) "test4"
2) "firstList"
redis 127.0.0.1:6379> RPUSH mylist four
(integer) 1
redis 127.0.0.1:6379> KEYS *
1) "test4"
2) "firstList"
3) "mylist"
redis 127.0.0.1:6379> LTRIM mylist -1 0
OK
redis 127.0.0.1:6379> KEYS *
1) "test4"
2) "firstList"
3) "mylist"

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 removed test4 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.

beware, oh ye who wanders here.

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.

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 list startIndex endIndex

Example assuming we have a redis list with key mylist containing 10 entries:

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.

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.

-1 refers to last element, - 2 penultimate element, etc. And (-n) is the first element.

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.

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.

I tried this and it works for me. Just change myList for your list name and execute it redis-cli KEYS "myList:*" | xargs redis-cli DEL

Just use LTRIM. ...Aaaand the magic here is to use start greater than 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).

Seems there is no atomic operation doing this. What I found out (example is in C# using StackExchange.Redis) is:

cacheDb.ListTrim(key, 0, 0); // remove all elements but the first one
cacheDb.ListLeftPop(key);    // then remove the first one

will remove all elements in two steps (where cacheDb is your database of type IDatabase which you are connected to, and key is of type RedisKey).

I think in cli syntax this will be:

LTRIM key 0 0
LPOP key

However, it seems to do the same as cacheDb.KeyDelete(key); (or in cli-syntax DEL key) - because in both cases the key isn't found any more.