LREM(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’– LREM β€” the "bye bestie" command πŸ’–

okay so this is lrem(3valkey) from the Valkey Command Manual, and it's literally the girl who shows up at the party and starts removing people from the list. sometimes it removes the whole list. dramatic? yes. iconic? also yes.


✨ NAME ✨

lrem β€” removes elements from a list. deletes the list if the last element was removed. it's giving cleanup era 🧹✨


🌸 SYNOPSIS 🌸

LREM key count element

πŸ¦‹ DESCRIPTION πŸ¦‹

so basically, lrem removes the first count occurrences of elements equal to element from the list stored at key. no cap.

and the count argument is the main character here, because it completely changes the vibe of the operation:

  • count > 0: remove elements equal to element, moving from head to tail. front to back, bestie. πŸ’…
  • count < 0: remove elements equal to element, moving from tail to head. back to front, reverse girlie. πŸ™ˆ
  • count = 0: remove all elements equal to element. all of them. every single one. periodt. πŸ’€

for example, LREM list -2 "hello" will remove the last two occurrences of "hello" in the list stored at list. that's the negative count doing its little reverse thing ✨

also important: non-existing keys are treated like empty lists, so when key does not exist, the command will always return 0. it's not ghosting you, there's just literally nothing there 🫢

πŸ’œ REPLY πŸ’œ

valkey-protocol(7) Integer reply: the number of removed elements. that's your receipt, bestie 🧾


πŸŽ€ COMPLEXITY πŸŽ€

O(N+M) where N is the length of the list and M is the number of elements removed. so the more you delete, the more work it does. she's thorough, not lazy.


🌈 ACL CATEGORIES 🌈

@list @slow @write


⭐ HISTORY ⭐

  • Available since: 1.0.0 β€” an OG, been here since the beginning, no notes πŸ‘‘

πŸ”₯ EXAMPLES πŸ”₯

127.0.0.1:6379> RPUSH mylist "hello"
(integer) 1
127.0.0.1:6379> RPUSH mylist "hello"
(integer) 2
127.0.0.1:6379> RPUSH mylist "foo"
(integer) 3
127.0.0.1:6379> RPUSH mylist "hello"
(integer) 4
127.0.0.1:6379> LREM mylist -2 "hello"
(integer) 2
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "foo"

see how she only took the last two "hello"s? the first one survived. survivor behavior fr fr 🌸


🧚 SEE ALSO 🧚


that's the whole page bestie πŸ’– this one's from Valkey 9.0.0, dated 2025-10-21, and it ate. go remove some elements. slay πŸŽ€βœ¨πŸ’…

πŸ“¦ where this page comes from