Delete all entries in the Redis list

Suppose you have a LIST data type in Redis. How to delete all entries? I have already tried this:

LTRIM key 0 0 LTRIM key -1 0 

Both of them leave the first element. This will leave all the elements:

 LTRIM key 0 -1 

I do not see a separate command to completely remove the list.

+69
list redis
Mar 22 '12 at 18:17
source share
6 answers

Remove the key and it will clear all items. The absence of a list is generally similar to the absence of any elements in it. Redis will not throw any exceptions when trying to access a non-existent key.

 DEL key 

Here are some console magazines.

 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)
+118
Mar 22 '12 at 18:22
source share

You can delete all values ​​in a list only if the list has 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" 

But , if the list has only one value, you should use

 DEL key 
+14
Oct 20 '13 at 17:46
source share

Just use LTRIM ... ahhh and the magic here is to use start more end .

 LTRIM key 99 0 

And ... boom! there goes the whole list of elements. Just * puff * right in front of your eyes !! There were no leftovers, and absolutely no questions.

Note: this will cause the key to be β€œdeleted” (as in the remote), as described here

... if the beginning is greater than the end of the list or the beginning> end, the result will be an empty list (which will remove the key).

+3
Jul 24 '18 at 11:40
source share

Answering my phone, I can’t format the answer correctly.

It may be a late answer, but I stick with it here just in case anyone still needs this functionality.

Short answer

ltrim mylist 0 - (n + 1) where mylist is the key and n is the length of the list.

Long answer

The way ltrim works is that it takes two indexes and returns the elements that fall between them, including indexes.

List of subscribers startIndex endIndex

Suppose we have a redis list with a key list containing 10 entries:

ltrim mylist 0 5 will clip the list to items starting at index 0 through index 5. And discard those that fall outside this range.

Fortunately, redis list operations support negative indexing, which is extremely useful in some situations. Usually when you do not know the length of the list.

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

Out of range indicators are not harmful. If the ending index is greater than the length of the list, redis treats it as equal to the last index.

This is why ltrim mylist 0, - (n +1) clears the list. He does this because (-n) is equivalent to index 0. Adding 1 to it does not leave a single element in this range, since it will be before the first element.

0
Jul 27. '17 at 12:14
source share

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 redis-cli KEYS "myList:*" | xargs redis-cli DEL redis-cli KEYS "myList:*" | xargs redis-cli DEL redis-cli KEYS "myList:*" | xargs redis-cli DEL

0
Jul 03 '18 at 8:09
source share

the accepted answer is incorrect. Let's pretend that

 redis 127.0.0.1:6379> KEYS * 1) "newKey" 2) "firstHash" 3) "secondList" 4) "test4" 5) "firstList" 6) "mylist" 

use the ahmed example, which is actually correct. now if i do this:

 DEL 'test4' 

i in total:

 1) "newKey" 2) "firstHash" 3) "secondList" 4) "firstList" 5) "mylist"` 

so I did not delete all entries from the list of "test4", I deleted test4 . Not the same. not at all. I have a small application in which list keys are hashes computed from several data (well, not all?), These lists are sometimes cleared, but the semantics of an empty list and a nonexistent list are very different. therefore, no, I do not want DEL 'myhash', I want to just delete all entries.

Beware, oh you who roamed here.

-one
Dec 30 '14 at 11:57
source share



All Articles