Easy Tutorial
❮ Transactions Watch Server Client Kill ❯

Redis Ltrim Command

Redis Lists

Redis Ltrim trims a list, meaning it keeps only the elements within the specified range and removes all elements outside that range.

An index of 0 represents the first element of the list, 1 represents the second element, and so on.

You can also use negative indices, where -1 represents the last element of the list, -2 represents the second-to-last element, and so on.

Syntax

The basic syntax for the redis Ltrim command is as follows:

redis 127.0.0.1:6379> LTRIM KEY_NAME START STOP

Available Versions

= 1.0.0

Return Value

Returns "OK" when the command executes successfully.

Example

redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 1
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 2
redis 127.0.0.1:6379> RPUSH mylist "foo"
(integer) 3
redis 127.0.0.1:6379> RPUSH mylist "bar"
(integer) 4
redis 127.0.0.1:6379> LTRIM mylist 1 -1
OK
redis 127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "foo"
3) "bar"

Redis Lists

❮ Transactions Watch Server Client Kill ❯