LTRIM(3valkey)
β¨ LTRIM (3valkey) β the valkey command manual, but make it cute β¨
okay bestie, pull up a chair, we're learning LTRIM π
it lives in the valkey command manual and it is genuinely one of the most useful lil guys in the whole list family.
πΈ NAME πΈ
ltrim β Removes elements from both ends a list. Deletes the list if all elements were trimmed.
yes, BOTH ends. it's giving scissors. it's giving "we're cutting the crusts off this sandwich" βοΈπ
π SYNOPSIS π
LTRIM key start stop
π DESCRIPTION π
So the vibe is: you have an existing list, and you wanna trim it down so it only contains the specified range of elements. Both start and stop are zero-based indexes β meaning 0 is the first element of the list (the head, the main character π), 1 is the next element, and so on down the line.
For example: LTRIM foobar 0 2 will modify the list stored at foobar so that only the first three elements of the list will remain.
start and end can also be negative numbers, which means they're counting offsets from the end of the list instead π¦ β where -1 is the last element of the list, -2 is the penultimate element, and so on.
Now here's the part where nobody yells at you for going out of range, because out of range indexes will not produce an error π:
- if
startis larger than the end of the list, orstart > end, the result will be an empty list β which causeskeyto be removed π - if
endis larger than the end of the list, Valkey will treat it like the last element of the list
A common use of LTRIM is together with valkey-lpush(7) LPUSH / valkey-rpush(7) RPUSH. For example:
LPUSH mylist someelement
LTRIM mylist 0 99
This pair of commands will push a new element on the list, while making sure that the list will not grow larger than 100 elements π₯ This is very useful when using Valkey to store logs for example. It is important to note that when used in this way LTRIM is an O(1) operation because in the average case just one element is removed from the tail of the list.
ngl this is the log-keeping hack and it absolutely ate π«Ά
π REPLY π
valkey-protocol(7) Simple string reply: OK.
β COMPLEXITY β
O(N) where N is the number of elements to be removed by the operation.
π ACL CATEGORIES π
@list @slow @write
π¦ HISTORY π¦
- Available since: 1.0.0
like... an icon from the very beginning, periodt π
β¨ EXAMPLES β¨
127.0.0.1:6379> RPUSH mylist "one"
(integer) 1
127.0.0.1:6379> RPUSH mylist "two"
(integer) 2
127.0.0.1:6379> RPUSH mylist "three"
(integer) 3
127.0.0.1:6379> LTRIM mylist 1 -1
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "two"
2) "three"
see how "one" just... left the chat π iconic behavior fr fr
π SEE ALSO π
- blmove(3valkey)
- blmpop(3valkey)
- blpop(3valkey)
- brpop(3valkey)
- brpoplpush(3valkey)
- lindex(3valkey)
- linsert(3valkey)
- llen(3valkey)
- lmove(3valkey)
- lmpop(3valkey)
- lpop(3valkey)
- lpos(3valkey)
- lpush(3valkey)
- lpushx(3valkey)
- lrange(3valkey)
- lrem(3valkey)
- lset(3valkey)
- rpop(3valkey)
- rpoplpush(3valkey)
- rpush(3valkey)
- rpushx(3valkey)
and that's the whole page, bestie π this was version 9.0.0, dated 2025-10-21, and honestly? she slayed. mwah π«Άβ¨
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms