XTRIM key <MAXLEN | MINID> [= | ~] threshold [LIMIT count]
- Available since:
- Time complexity:
- O(N), with N being the number of evicted entries. Constant times are very small however, since entries are organized in macro nodes containing multiple entries that can be released with a single deallocation.
- ACL categories:
XTRIM trims the stream by evicting older entries (entries with lower IDs) if needed.
Trimming the stream can be done using one of these strategies:
MAXLEN: Evicts entries as long as the stream's length exceeds the specified
thresholdis a positive integer.
MINID: Evicts entries with IDs lower than
thresholdis a stream ID.
For example, this will trim the stream to exactly the latest 1000 items:
XTRIM mystream MAXLEN 1000
Whereas in this example, all entries that have an ID lower than 649085820-0 will be evicted:
XTRIM mystream MINID 649085820
By default, or when provided with the optional
= argument, the command performs exact trimming.
Depending on the strategy, exact trimming means:
MAXLEN: the trimmed stream's length will be exactly the minimum between its original length and the specified
MINID: the oldest ID in the stream will be exactly the maximum between its original oldest ID and the specified
Nearly exact trimming
Because exact trimming may require additional effort from the Redis server, the optional
~ argument can be provided to make it more efficient.
XTRIM mystream MAXLEN ~ 1000
~ argument between the
MAXLEN strategy and the
threshold means that the user is requesting to trim the stream so its length is at least the
threshold, but possibly slightly more.
In this case, Redis will stop trimming early when performance can be gained (for example, when a whole macro node in the data structure can't be removed).
This makes trimming much more efficient, and it is usually what you want, although after trimming, the stream may have few tens of additional entries over the
Another way to control the amount of work done by the command when using the
~, is the
When used, it specifies the maximal
count of entries that will be evicted.
count aren't specified, the default value of 100 * the number of entries in a macro node will be implicitly used as the
Specifying the value 0 as
count disables the limiting mechanism entirely.
RESP2/RESP3 ReplyInteger reply: The number of entries deleted from the stream.
- Starting with Redis version 6.2.0: Added the
MINIDtrimming strategy and the