16

How to remove element from list in Redis by value?

For exmaple, I have:

127.0.0.1:6379> lrange POST:544 0 -1
1) "1"
2) "2"
3) "36"
127.0.0.1:6379> 

I know only value 36, not index. Can I remove element from list by value?

Faud
  • 451
  • 3
  • 7
  • 16

2 Answers2

20

http://redis.io/commands/lrem

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

Karthikeyan Gopall
  • 5,469
  • 2
  • 19
  • 35
  • 1
    This only works when numeric values are stored in the list right? Tried it with string types stored in the list, but it did not work. Instead I did change the list into a set and now for example this works `SREM myset "four"`. – Baran May 10 '18 at 08:16
6

If the list contains strings then you may enclose it with double quotes then it works!

eg: sampleList ["one", "two", "three", "four"] If you want to remove "three" then use:

LREM sampleList 1 "three"
Duncanmoo
  • 3,535
  • 2
  • 31
  • 32
prince_29
  • 81
  • 1
  • 4