Redis - List Ltrim Command



Redis LTRIM command trims an existing list so that it contains only the specified range of elements. 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.

Return Value

String reply, OK.

Syntax

Following is the basic syntax of redis LTRIM command.

redis 127.0.0.1:6379> LTRIM KEY_NAME START STOP

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.htm
Advertisements