# Retention

Customers set conversation retention at agent, workspace or organization level. The nearest policy applies unless a higher-level policy is locked. Each policy requires a retention period. Polyant has no deployment-wide default for conversation retention. Without an applicable enabled policy, the scheduled purge leaves conversation data in place indefinitely. When a purge runs, it permanently deletes the expired conversations and related records, subject to the policy's option to preserve governance events.

The separate [Analytics retention](https://docs.polyant.ai/admin-panel/installation-policies) policy defaults to 90 days for telemetry, including tool audit rows.

> **Managed services.** The retention engine is available with Polyant managed services.

Retention is set in three places: the **Retention** section of an agent's *Governance* group (`?tab=retention`), the workspace's **Data retention** page (`/organizations/{org}/workspaces/{ws}/settings/retention`), and the organization's **Data retention** page (`/organizations/{org}/retention`), both in their sidebar's *Governance* group. A policy set on the organization or a workspace applies to every agent below it that has no nearer policy of its own (see [Organization and workspace policies](#organization-and-workspace-policies)).

It sat briefly inside a combined **Data & privacy** section with the [messaging opt-out](https://docs.polyant.ai/admin-panel/privacy), and was split back out. The two are not the same concern: an opt-out is a per-contact switch an operator flips daily, a retention policy is a destructive schedule nobody touches for months.

> **This is destructive and has no undo.** A purge is a hard delete, not an archive. Export anything you need to keep before running one.

On an agent, reading the policy and the run history requires `conversation:read`. Saving, deleting, or running the policy requires `conversation:delete`.

## The policy

An agent follows one policy: its own, its workspace's or its organization's, whichever is nearest. No policy at any level means no purge: an agent's data is kept indefinitely until a policy applies to it and is enabled.

- **Enable retention** - whether the scheduler runs this policy.
- **Retention period** - pick a preset or enter a custom number of days. It is **required**: there is no deployment-wide default behind it, and a policy that does not state a window is rejected on save. The most destructive value in the product is not one a policy may leave to a setting somewhere else, where changing it would alter what every agent deletes with nothing recording the change.
- **Schedule (cron expression)** and **Timezone** - when the purge runs. The schedule is **cron-only**: an interval or one-shot schedule would fire once and then freeze the policy while still reading as enabled, so the API rejects it. An invalid cron expression is rejected on save.
- **Preserve governance events** - keeps `governance_events` rows for the purged conversations. Use it when governance records must outlive the conversation for legal retention.

The panel shows the policy's **next run** and **last run** with its status. **Reset to default** deletes the agent's own policy; the agent then follows the workspace's or the organization's policy, if there is one.

## Organization and workspace policies

The organization and each workspace can hold one policy, with the same fields as an agent's. The organization's applies to every agent of the organization, the workspace's to the agents of that workspace, and the nearest level wins: an agent's own policy over its workspace's, a workspace's over the organization's. A nearer policy wins even when it is disabled, so an agent with its own disabled policy is not purged by the workspace's or the organization's (unless a lock above stops it). Nothing is copied into the agents. The policy in force is worked out each time it is read, so changing the organization's policy changes every agent that follows it at once.

Each of the two pages also has a lock: **Allow a different value in workspaces and agents** on the organization, **Allow a different value for individual agents** on the workspace. It is on by default. Turned off, the policy applies to everyone below it:

- a lower level cannot save a policy of its own: the API answers `409` with `code: "setting_locked"` and names the level that holds the lock, and the panel shows the fields read only with **Locked by the organization** (or **by the workspace**) and the value in force;
- a policy a lower level saved before the lock stays stored but is not applied, and the panel says so; removing it is always allowed, since it only clears something that no longer applies.

On an agent or a workspace that follows a level above, the panel shows one line with the value it inherits and an **Override** link. Once the level holds its own policy, a notice says so and offers **Back to the inherited value**.

When an organization or workspace policy fires, it purges every active agent whose policy in force comes from it, one after the other. An agent with its own policy, or with a workspace policy nearer than the organization's, is not touched by that run. Each agent still gets its own row in the run history, and the organization or workspace policy reports an error when any agent failed while the others are still purged. An agent's own policy that a lock above has made inactive is skipped by the scheduler.

| Level | Read | Write (save, remove) |
|---|---|---|
| Organization | `org.governance:read` (every role) | `org.governance:write` and `conversation:delete` (Admin and Owner) |
| Workspace | `conversation:read` | `conversation:delete` |
| Agent | `conversation:read` | `conversation:delete` |

The workspace's **Data retention** entry is listed to callers who hold `conversation:read` there, and changing it needs `conversation:delete`. The organization's page is readable by every role and editable by Admin and Owner. **Dry run** and **Run now** exist on the agent only, and run the policy in force for that agent, wherever it comes from.

The agent's [Status](https://docs.polyant.ai/admin-panel/instances#status) section and its [compliance snapshot](https://docs.polyant.ai/admin-panel/compliance#configuration-snapshots) report the policy in force, inherited ones included.

## Cutoff and what gets deleted

The cutoff is computed once per run as *now minus the retention window*, and a conversation is expired when its **last activity** is older than that cutoff. The same cutoff is used by the audit row and the deletion, so the two never drift.

Deleting a conversation cascades, in one transaction per batch (500 conversations at a time), to:

- its messages;
- the AI-gateway call log rows and pipeline traces for it;
- tool audit logs and hook executions;
- the raw model exchanges debug mode kept for it, and the voice module's decisions for its calls;
- governance events - **unless** *Preserve governance events* is on;
- memories extracted from it;
- its conversation state and its encrypted per-conversation credentials;
- its `email_threads` rows - the `Message-ID` index the [email channel](https://docs.polyant.ai/admin-panel/channels#email) threads on, which would otherwise outlive the conversation and let a later reply resolve to one that no longer exists;
- the conversation row itself.

Conversations are the unit of deletion: an agent's prompts, tools, skills, secrets, channels, and knowledge base are never touched by retention. Because only *extracted memories* are removed, keyword search over the remaining raw messages keeps working.

## Running it

Two buttons under **Run purge**, on the agent's section:

- **Dry run (preview)** - computes the cutoff and reports how many conversations would be purged, without deleting anything. Do this first.
- **Run now** - purges immediately, behind a confirmation dialog. It requires a policy in force; run it after saving, not with unsaved edits on screen.

The scheduler checks for due policies every 30 seconds and catches up on runs that came due while the process was down. Due policies run one at a time - each purge is already batched and DB-heavy.

## Run history

Every pass, scheduled or manual, is audited in the runs table: cutoff, trigger (scheduled or manual), dry-run flag, status, per-table deleted counts, conversations scanned, duration, and any error. The audit stores **counts only** - never the deleted content.

A running purge reports that it is alive every minute. If the process running it stops (a deploy, a crash), the policy is released after five missed reports, marked with an error that says the run was interrupted, and runs again on its schedule: a purge is safe to repeat. The agent's Status section flags a pass that has reported nothing for over ten minutes.

Configuration writes at every level and purge runs are also recorded in the [management audit log](https://docs.polyant.ai/admin-panel/audit-logs) with the acting operator. Purge runs also appear in the [Monitor](https://docs.polyant.ai/admin-panel/activity).

## See also

- [Privacy](https://docs.polyant.ai/admin-panel/privacy) - messaging opt-out.
- [Conversations](https://docs.polyant.ai/admin-panel/conversations) - deleting a single conversation by hand, same cascade.
- [Compliance](https://docs.polyant.ai/admin-panel/compliance) - snapshots and reports, which are not affected by a purge.
