Back to Redis

LREM

content/commands/lrem.md

latest1.5 KB
Original Source

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-cli %}} RPUSH mylist "hello" RPUSH mylist "hello" RPUSH mylist "foo" RPUSH mylist "hello" LREM mylist -2 "hello" LRANGE mylist 0 -1 {{% /redis-cli %}}

Redis Software and Redis Cloud compatibility

| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | |

Return information

{{< multitabs id="lrem-return-info" tab1="RESP2" tab2="RESP3" >}}

Integer reply: the number of removed elements.

-tab-sep-

Integer reply: the number of removed elements.

{{< /multitabs >}}