Is it possible to remove an element at a specific index in an redis list ? Not really finding what I want. There is trim which allows you to select an specific set of elements, LREM allows you to delete an item in a list by value, but I dont have the value.
I found an hack where you use LSET to change the value of the element to a UID or string e.g. 'DELETED', and the you call LREM on this value. This just feels a bit dirty though.
You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output.
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.
Removes and returns the first elements of the list stored at key . By default, the command pops a single element from the beginning of the list. When provided with the optional count argument, the reply will consist of up to count elements, depending on the list's length.
Creating Lists Redis reads lists from left to right, and you can add new list elements to the head of a list (the “left” end) with the lpush command or the tail (the “right” end) with rpush . You can also use lpush or rpush to create a new list: lpush key value.
So the only way to accomplish what I wanted was to set the value at the index to a pre determined string and then do an removal by value.
see discussion here https://groups.google.com/forum/#!topic/redis-db/c-IpJ0YWa9I
in ruby as follows
@redis.lset("#{@namespace}/#{specified_queue}", index, "DELETED")
@redis.lrem("#{@namespace}/#{specified_queue}", 1, "DELETED")
LSET docs http://redis.io/commands/lset LREM docs http://redis.io/commands/lrem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With