Delete all entries in a Redis list

Multi tool use
Multi tool use


Delete all entries in a Redis list



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.





@DavidJames: Consider it another way of spelling "foo." I'm following the convention used in Redis's own documentation: redis.io/commands
– Paul A Jungwirth
Aug 13 '12 at 19:59





Yes, in redis, all data structures are keys. That doesn't mean 'key' is useful in an example. Quite the opposite, I think. Using mylist would make your question clearer. For example, redis.io/commands/ltrim writes: LTRIM mylist 1 -1. The page you cite is a command reference and should not be considered a "convention" for making good examples.
– David J.
Aug 13 '12 at 20:41



mylist


LTRIM mylist 1 -1




5 Answers
5



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.





Is there a way to do that in one atomic operation? (both "LRANGE names 0 2" and "DEL names")
– refaelos
Oct 14 '13 at 10:44





Yes. See Redis transactions - redis.io/topics/transactions
– Anurag
Oct 14 '13 at 21:20





One thing, using Del may create extra overhead, Time complexity: O(N) where N is the number of keys that will be removed. When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash. Removing a single key that holds a string value is O(1). The docs
– ChewOnThis_Trident
May 15 '14 at 22:46



may create extra overhead





this method is dangerous as it also remove the ttl of the key
– Saitama
Mar 1 '16 at 15:11



You can remove all values in the 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 one value only , you must use


DEL key



Am answering on my phone so can't actually format the answer well.



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.





Thanks for sharing your insights. As an extension to your answer, you could simply supply a value of N that is sufficiently larger than the list to guarantee that all items are deleted. For example ltrim mylist 0 -99999.
– Dave Johnson
Jan 6 at 0:09



ltrim mylist 0 -99999



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



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.





If you look at the page cited below you'll see that in fact the semantics ARE the same! Taken from: The LPUSH command inserts a new element on the head, while RPUSH inserts a new element on the tail. A new list is created when one of this operations is performed against an empty key. Similarly the key is removed from the key space if a list operation will empty the list. These are very handy semantics since all the list commands will behave exactly like they were called with an empty list if called with a non-existing key as argument.
– Dominic
Jan 20 '14 at 8:46



The LPUSH command inserts a new element on the head, while RPUSH inserts a new element on the tail. A new list is created when one of this operations is performed against an empty key. Similarly the key is removed from the key space if a list operation will empty the list. These are very handy semantics since all the list commands will behave exactly like they were called with an empty list if called with a non-existing key as argument.





from the horses mouth: "Similarly the key is removed from the key space if a list operation will empty the list." so, is { , , } === { } ? not for me. i guess we're in different topological spaces.... :)
– deimosaffair
Jan 22 '14 at 8:59






Well the initial question was how to delete an entire List in redis. so if you either delete the key or delete all entries and have the key automatically removed by redis gives the same result. So I'm not sure what you mean with { , , } === { }.
– Dominic
Jan 23 '14 at 9:49





Removing the last element from list is equal to deleting the key. This link even same for LIST as well (tested with RPOPLPUSH) bennadel.com/blog/…
– Kanagavelu Sugumar
Aug 3 '17 at 16:45







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

EY PlDlsc7husQLbd3tc PzSgcVTwXVbY5NOa2E7Bg66wQm 7B2Il
zycC7pnsD QO2cKw8d GZei06SUEQ,MuOxZNfAj4t6Mzun4D9pJUq9MPeH jMHj8ri6 7KZV0p jg xe,4kr 0U

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications