> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# XTRIM

> Trims the stream by removing entries to keep it at a reasonable size.

## Arguments

<ParamField body="key" type="string" required>
  The key of the stream.
</ParamField>

<ParamField body="options" type="object" required>
  <Expandable title="properties">
    <ParamField body="strategy" type="'MAXLEN' | 'MINID'" required>
      The trimming strategy:

      * `MAXLEN`: Trim based on the maximum number of entries
      * `MINID`: Trim based on the minimum ID
    </ParamField>

    <ParamField body="threshold" type="number | string" required>
      The threshold value for trimming:

      * For `MAXLEN`: The maximum number of entries to keep (number)
      * For `MINID`: The minimum ID to keep (string). Entries with IDs lower than this will be removed
    </ParamField>

    <ParamField body="exactness" type="'~' | '='">
      Use `~` for approximate trimming (more efficient, default) or `=` for exact trimming.
    </ParamField>

    <ParamField body="limit" type="number">
      Limit how many entries will be trimmed at most (only valid with approximate trimming `~`).
    </ParamField>
  </Expandable>
</ParamField>

## Response

<ResponseField type="number">
  The number of entries removed from the stream.
</ResponseField>

<RequestExample>
  ```ts Approximate trim by max length theme={"system"}
  const result = await redis.xtrim("mystream", {
    strategy: "MAXLEN",
    threshold: 100,
    exactness: "~"
  });
  ```

  ```ts Exact trim by max length theme={"system"}
  const result = await redis.xtrim("mystream", {
    strategy: "MAXLEN", 
    threshold: 50,
    exactness: "="
  });
  ```

  ```ts Trim by minimum ID theme={"system"}
  const result = await redis.xtrim("mystream", {
    strategy: "MINID",
    threshold: "1638360173533-0",
    exactness: "="
  });
  ```

  ```ts Approximate trim with limit theme={"system"}
  const result = await redis.xtrim("mystream", {
    strategy: "MAXLEN",
    threshold: 1000,
    exactness: "~",
    limit: 100
  });
  ```
</RequestExample>
