Delete all entries in a Redis list

Solution 1:

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)

Solution 2:

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"

Solution 3:

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