w3resource

Redis Lists: LREM

Redis LREM Command

Redis LREM command is used to remove the first count occurrences of elements equal to the value from the list stored at key. The count argument influences the operation describes as below:

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

Here are some common uses and scenarios for the "LREM" command:

  • Data Cleanup: Remove unwanted or duplicate elements from a list.
  • Task Management: Delete specific tasks from a queue that are no longer needed.
  • Event Handling: Clear specific events or messages from an event list.
  • List Maintenance: Maintain list integrity by removing erroneous or outdated entries.
  • Conditional Deletion: Remove a specific number of occurrences of an element, allowing for precise list management.

Syntax:

LREM KEY_NAME COUNT VALUE   

Available since

1.0.0.

Return Value

Integer replies the number of removed elements.

Return Value Type

Integer

Example: Redis LREM

127.0.0.1:6379> LPUSH mycolor red red red green
(integer) 4
127.0.0.1:6379> LRANGE mycolor 0 -1
1) "green"
2) "red"
3) "red"
4) "red"
127.0.0.1:6379> LREM mycolor 1 red
(integer) 1
127.0.0.1:6379> LRANGE mycolor 0 -1
1) "green"
2) "red"
3) "red"
127.0.0.1:6379> LREM mycolor 0 red
(integer) 2
127.0.0.1:6379> LRANGE mycolor 0 -1
1) "green"

Example: Redis LREM: When used in the right to delete from negative

127.0.0.1:6379> LPUSH mycolor red red red green
(integer) 4
127.0.0.1:6379> LRANGE mycolor 0 -1
1) "green"
2) "red"
3) "red"
4) "red"
127.0.0.1:6379> LREM mycolor -2 red
(integer) 2
127.0.0.1:6379> LRANGE mycolor 0 -1
1) "green"
2) "red"

Previous: LPUSHX
Next: LSET



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/redis/redis-lrem-key-count-value.php