LREM

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Remove by value

Usage is lrem key count value.
If count is positive, the specified value is removed from the left of the list up to count times.
If count is 0, all matching values are removed. Returns the number of removed values.

Example

Command>lpush mylist value2 value2 value2 value1
Result>4
Command>lrange mylist 0 -1
Result>0) value1
1) value2
2) value2
3) value2
Command>lrem mylist 1 value2
Result>1
Command>lrange mylist 0 -1
Result>0) value1
1) value2
2) value2
Command>lrem mylist 0 value2
Result>2   Returns the number of removed values
Command>lrange mylist 0 -1
Result>0) value1

View animation


Use a negative count to remove from the right

If count is negative, values are removed from the right up to |count| times.

Example

Command>lpush mylist value2 value2 value2 value1
Result>4
Command>lrem mylist -2 value2
Result>2
Command>lrange mylist 0 -1
Result>0) value1
1) value2

View animation


Remove by index

You can specify an index to remove a value. Indexes start at 0. The last index can be -1 or the result of LLEN. A reverse index can be used.
When you know the index, it is faster than using the value.

Example

Command>lrem mylist 1 by index
Result>2
Command>lrem mylist -1 by index
Result>1
This feature is available on Enterprise servers.

Syntax

LREM key   [count value] | [<index> by index]

  • Available since version 1.0.0.
  • Time complexity is O(N). N is the number of elements.
  • Returns the number of removed elements.
Related commands LTRIM, LINSERT, LSET, DEL
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

<< LINSERT LREM LRM >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.