LTRIM

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Remove data outside the specified index range

Usage is ltrim key start stop.
Useful when you want to keep only the specified range and delete the rest.

Example

Command>lpush mylist value1 value2 value3 value4
Result>4
Command>lrange mylist 0 -1
Result>0) value4
1) value3
2) value2
3) value1
Command>ltrim key 1 2
Result>OK
Command>lrange key 0 -1
Result>1) value3
2) value2

View animation



Using negative indexes

Use negative numbers for reverse indexes.

Example

Command>lpush mylist value1 value2 value3 value4
Result>4
Command>ltrim key -2 -1
Result>OK
Command>lrange key 0 -1
Result>1) value4
2) value3

View animation



Be careful with the range

If you specify a range that does not exist, all data is deleted. Be careful.

Example

Command>ltrim key 10 10
Result>OK   All items were deleted
Command>lrange key 0 -1
Result>   No result


Syntax

LTRIM key start stop

  • Available since version 1.0.0.
  • Time complexity is O(N). N is the number of elements to remove.
Related commands LREM, LINSERT, LSET, DEL
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

<< LRM LTRIM LMOVE >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.