Use XTRIM to cap the size of a stream by removing old entries.
MAXLEN keeps at most the given number of entries, dropping the oldest ones, while MINID drops every entry with an ID below a threshold, which is how you trim by age since IDs begin with a millisecond timestamp. The reply is the number of entries removed.
~ makes the trim approximate: the server stops at a convenient boundary and may leave a few extra entries, which is much cheaper on large streams and is the right default for background maintenance. = trims exactly, and LIMIT caps how many entries a single call may evict, which keeps one call from blocking for a long time.
KEEPREF, DELREF, and ACKED decide what happens to consumer group references of the removed entries: KEEPREF, the default, leaves them in place, DELREF removes them from every group's pending list, and ACKED only removes entries that all groups have read and acknowledged. XADD accepts the same options so a stream can be trimmed as it is written to.
Syntax#
Arguments#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<key> | Yes | No | Redis key targeted by the command. |
(MAXLEN | MINID) [= | ~] <threshold> [LIMIT <count>] [KEEPREF | DELREF | ACKED] | Yes | No | Trim the stream. MAXLEN caps the number of entries; MINID drops entries with a lower ID. = trims exactly and is the default; ~ trims approximately and is required before LIMIT, which caps how many entries a single call evicts. KEEPREF (the default) leaves consumer-group references to the deleted entries in place, DELREF removes those references too, and ACKED only removes entries that every group has read and acknowledged. |
Response#
The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.
| Protocol | Reply |
|---|---|
| RESP2 | Integer |
| RESP3 | Integer |
Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
Examples#
TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.