w3resource

Redis Lists: LTRIM

LTRIM key start stop

Redis LTRIM command is used to trim an existing list so that it will contain only the specified range of elements specified. Both start and stop are zero-based indexes, where 0 is the first element of the list (the head), 1 the next element and so on.

Syntax:

redis 127.0.0.1:6379> LTRIM KEY_NAME START STOP

Available since

1.0.0.

Return Value

String reply, OK

Return Value Type

String

Example: Redis LTRIM: Delete your data outside the range specified by index

127.0.0.1:6379> LPUSH mycolor1 white black red blue
(integer) 4
127.0.0.1:6379> LRANGE mycolor1 0 -1
1) "blue"
2) "red"
3) "black"
4) "white"
127.0.0.1:6379> LTRIM mycolor1 1 2
OK
127.0.0.1:6379> LRANGE mycolor1 0 -1
1) "red"
2) "black"

Example: Redis LTRIM: Use negative

127.0.0.1:6379> LPUSH mycolor1 white black red blue
(integer) 4
127.0.0.1:6379> LTRIM mycolor1 -2 -1
OK
127.0.0.1:6379> LRANGE mycolor1 0 -1
1) "black"
2) "white"

Example: Redis LTRIM: Range Selection Caution

127.0.0.1:6379> LPUSH mycolor1 white black red blue
(integer) 4
127.0.0.1:6379> LTRIM mycolor1 10 10
OK
127.0.0.1:6379> LRANGE mycolor1 0 -1
(empty list or set)

Previous: LRANGE
Next: RPOP



Follow us on Facebook and Twitter for latest update.