# Metadata retention

**Last updated:** 2026-11-26T02:16:00-03:00 (UTC-3)

Usage events grow forever if nobody deletes them. Retention removes, in small batches and without getting in the way of inference, the usage events older than the period you set, along with the valuations and finalized receipts that depend on them. Flag: `metadata_retention`, which requires PostgreSQL.

**Nothing is deleted by default.** With the flag off or `retain_days: 0`, everything is preserved.

## How to turn it on

```yaml
features:
  metadata_retention: true
metadata_retention:
  revision: 1          # increase it on every YAML change
  retain_days: 90      # 7 to 3,650; 0 preserves everything
  batch_size: 250      # 1 to 1,000 events per batch
  interval: 10m        # 1 min to 24 h between batches
```

90 days is only an example: choose the period with whoever is accountable for audit and backup.

The existing defaults and bounds are shared by YAML, the administrative API and the existing console controls:

| Field | Default and accepted policy |
| --- | --- |
| `retain_days` | `0` preserves; positive values `7`–`3650` |
| `batch_size` | Omitted or `0` uses `250`; positive values `1`–`1000` |
| `interval` | Empty uses `10m`; valid Go duration `1m`–`24h` |
| `revision` | Default `0`; allowed `0`–`2147483647`; a positive YAML period requires a positive revision and the monotonic update rule below |

The interval retains full Go duration syntax without an extra thirty-two-character form limit. Defaults are selections, not new retention periods or permission to delete. The existing managed policy writer and YAML revision remain separate controls; see [parameter definitions](/docs/reference/parameter-registry/).

**In the console:** **Metadata retention** shows the policy in effect, its source (YAML or console), the cadence, the last run, and a preview of what would be removed. Editing requires the global admin (`retention:manage`). Any positive period asks you to check **I authorize irreversible deletion of expired usage, valuation and finalized receipt metadata**, a box that starts unchecked on every edit.

**Through the API:**

| Endpoint | What it does |
| --- | --- |
| `GET /api/v1/admin/retention` | Policy, version, and schedule; the YAML `revision` in force is `metadata_retention.revision` |
| `PUT /api/v1/admin/retention` | `{base_fingerprint, version, policy:{retain_days,batch_size,interval}, confirm_delete_expired_metadata}` |
| `DELETE /api/v1/admin/retention` | Reverts to the YAML (with the same confirmation, if the YAML has a positive period) |
| `GET /api/v1/admin/retention/preview` | A sample of up to 1,000 eligible events, without deleting anything |

There is no "delete everything now": the first run waits for the interval.

## What is removed and what stays

| Data | Outcome |
| --- | --- |
| Usage event without a receipt, older than the period | Removed |
| Event with a finalized receipt (`completed` or `indeterminate`), with occurrence **and** finalization older than the period | Removed, along with the receipt |
| The event's valuation | Removed in the same transaction |
| Receipt still without a final result | **Preserved** |
| Frozen prices and price book history | **Preserved** |
| Audit, identities, configuration, counters, and the [budgets](/docs/features/budgets-and-tpm/) ledger | **Never removed** by this function |

The period is measured by the PostgreSQL clock. An old request that is finalized now starts counting again from its finalization.

Analytics warns you when a period was affected by retention, instead of showing the empty range as "no traffic".

## How it works

- Each replica checks every minute whether it is time to run; a shared lock guarantees one run at a time across the cluster.
- **One batch per run**, taking up to 5 s. Rows in use are skipped and left for the next batch.
- The batch's removal, count, and audit are written together; any failure rolls back the whole batch.
- Retention never runs on the inference path.

Batches and time are bounded, but the cost depends on the volume and the database indexes. Reclaiming disk space (`VACUUM`) and purging data from backups are operations tasks.

## YAML and multiple replicas

- What is saved in the console takes precedence over the YAML; removing the override reverts to the YAML.
- When you change the YAML, **increase `revision`**. A replica with a lower revision does not run retention. Two different baselines with the same revision both stop until a higher revision resolves it.
- Concurrent changes get `409`, and the console keeps your draft.

## Pausing and rolling back

- To pause, save a period of zero or turn the flag off. What has already been deleted does not come back.
- When you roll back to an earlier version of the gateway, do not drop tables or versions.

## Limits

- An active [legal hold](/docs/features/data-governance/#legal-holds) suspends this purge: the batch is skipped and runs on a later tick once every hold is released. Contractual retention of the audit log is [audit retention](/docs/features/data-governance/#audit-retention).
- No anonymization.
- The preview is a sample: with `truncated: true`, the numbers are a minimum, not the total.
- The budgets ledger has its own archiving, in [financial reconciliation](/docs/features/quota-finance/#archiving-the-ledger).

Captured prompts and answers ([content capture](/docs/features/content-capture/)) are not usage metadata: each record carries its own expiry, from its team policy, and is purged every minute whatever this setting says.

## Admin API routes

Every route below needs a console session or an admin Bearer token, the role's permission and, for writes, the `X-CSRF-Token` header; the full contract is in the [API reference](/docs/api/).

| Route | What it does |
| --- | --- |
| `PUT /api/v1/admin/retention` | Stores the retention policy of expired usage metadata at the current version |

## See also

- [Durable receipts](/docs/features/durable-usage/).
- [Analytics](/docs/features/analytics-experience/).
- [Operations](/docs/operate/operations/).

## Evaluation results

With `evaluation_result_retention` (T186.016, operator control, off by default) the same window also removes evaluation results and dataset runs, in bounded batches, after each pass; human annotations, annotated or retained traces and the datasets themselves are kept. See [retention of results](/docs/features/public-trace-api/#retention-of-results).
