Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all entries in a Redis list

Tags:

list

redis

Suppose you have a LIST datatype in Redis. How do you delete all its entries? I've tried this already:

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.

like image 641
Paul A Jungwirth Avatar asked Mar 22 '12 18:03

Paul A Jungwirth


People also ask

How do I empty a list in Redis?

To delete a large list in Redis: Rename the key to a unique, namespaced key so that the list appears “deleted” to other Redis clients immediately. Incrementally delete elements from the list in small batches until it is empty.

How do I delete all keys?

If there's only one line of text in the document, or you want to delete all text, press Ctrl + A key to select all text. Once highlighted, press delete to delete everything.

How do I delete all keys matching a pattern in Redis?

Redis does not offer a way to bulk delete keys. You can however use redis-cli and a little bit of command line magic to bulk delete keys without blocking redis. This command will delete all keys matching users:* If you are in redis 4.0 or above, you can use the unlink command instead to delete keys in the background.

How do I delete multiple keys in Redis?

We can use keys command like below to delete all the keys which match the given patters “ user*" from the Redis. Note :- Not recommended on production Redis instance. I have written Lua script to delete multiple keys by pattern . Script uses the scan command to delete the Redis cache key in incremental iteration.


2 Answers

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) 
like image 188
Anurag Avatar answered Sep 18 '22 09:09

Anurag


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" 
like image 20
ahmed hamdy Avatar answered Sep 18 '22 09:09

ahmed hamdy