Remove Element at Specific Index from Redis List

Remove element at specific index from redis list

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

How to remove element from list in Redis by value?

http://redis.io/commands/lrem

Lrem is what you are looking for. use LREM POST:544 1 36.

Delete all entries in a Redis list

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)

How to delete object Redis list item?

Do you have control over the code that inserts the JSON? If so, you'll need to make sure they generate the same strings. LREM requires an exact match to work. That said, you might want to consider a different data structure or maybe a combination of data structures.

One option is that you could store the JSON as a String and then the List could contain the keys to those Strings. Then, when you delete a comment, you call LREM to remove it from the List and UNLINK or DEL to remove the JSON. The List serves to store an ordered index of the comments. The comments themselves are each stored in a String.

If you can use RedisJSON, you could just store a JSON document with an array of the comments. That would give you ordered data and the data itself and then you could delete a particular comment using JSON.DEL and JSONPath.



Related Topics



Leave a reply



Submit