# LTRIM

> Trim a list so it keeps only a range of elements.

URL: https://docs.hotkv.com/commands/ltrim/

```
LTRIM key start stop
```

- Group: List
- Complexity: O(N) where N is the number of elements removed
- HotKV Cloud: all plans
- On-Premise: no license needed

`LTRIM` keeps only the elements of the list at `key` from index `start` to
index `stop`, both included, and removes the rest. Indexes follow the same
rules as [LRANGE](https://docs.hotkv.com/commands/lrange/index.md): they start at `0`, negative values count
from the end, and out-of-range values are clamped. If the range is empty, the
whole list is removed and the key is deleted.

A common pattern is to push and then trim, to keep a capped list of the most
recent items:

```
LPUSH recent:events <event>
LTRIM recent:events 0 99
```

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Success (including a missing key) | Simple string `OK` | Same |

Errors:

- `ERR value is not an integer or out of range` if `start` or `stop` is not an
  integer. This is checked before the key is looked up.
- `WRONGTYPE` if `key` holds a value that is not a list.

## Examples

```
127.0.0.1:6379> RPUSH letters a b c d e
(integer) 5
127.0.0.1:6379> LTRIM letters 1 -2
OK
127.0.0.1:6379> LRANGE letters 0 -1
1) "b"
2) "c"
3) "d"
127.0.0.1:6379> LTRIM letters 5 10
OK
127.0.0.1:6379> EXISTS letters
(integer) 0
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[LRANGE](https://docs.hotkv.com/commands/lrange/index.md), [LREM](https://docs.hotkv.com/commands/lrem/index.md), [LPUSH](https://docs.hotkv.com/commands/lpush/index.md),
[LLEN](https://docs.hotkv.com/commands/llen/index.md)