Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Redis Ltrim Command

Redis Ltrim Command

Redis List

Redis Ltrim trims a list so that it keeps only the elements within the specified range; all elements outside the range are removed.

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

Syntax

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



redis 127.0.0.1:6379> LTRIM KEY_NAME START STOP

Available Version

= 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 List