LREM

Syntax
LREM key count element
Available since:
1.0.0
Time complexity:
O(N+M) where N is the length of the list and M is the number of elements removed.
ACL categories:
@write, @list, @slow,

Removes the first count occurrences of elements equal to element from the list stored at key. The count argument influences the operation in the following ways:

  • count > 0: Remove elements equal to element moving from head to tail.
  • count < 0: Remove elements equal to element moving from tail to head.
  • count = 0: Remove all elements equal to element.

For example, LREM list -2 "hello" will remove the last two occurrences of "hello" in the list stored at list.

Note that non-existing keys are treated like empty lists, so when key does not exist, the command will always return 0.

Examples

redis> RPUSH mylist "hello"
(integer) 11
redis> RPUSH mylist "hello"
(integer) 6
redis> RPUSH mylist "foo"
1) (integer) 8
2) (integer) 9
3) (integer) 10
redis> RPUSH mylist "hello"
Cannot destructure property 'error' of 'replies[i]' as it is undefined.
redis> RPUSH mylist "hello"
Cannot destructure property 'error' of 'replies[i]' as it is undefined.
redis> RPUSH mylist "foo"
Cannot destructure property 'error' of 'replies[i]' as it is undefined.
redis> RPUSH mylist "hello"
Cannot destructure property 'error' of 'replies[i]' as it is undefined.
redis> LREM mylist -2 "hello"
Cannot destructure property 'error' of 'replies[i]' as it is undefined.
redis> LRANGE mylist 0 -1
Cannot destructure property 'error' of 'replies[i]' as it is undefined.
redis>

RESP2/RESP3 Reply

Integer reply: the number of removed elements.
RATE THIS PAGE
Back to top ↑