2026-08-27 12:34:13 -04:00
---
2026-09-29 20:13:02 -04:00
title : Data retention
2026-08-27 12:34:13 -04:00
---
2025-12-02 17:47:36 +02:00
2026-10-14 14:29:20 -04:00
Coder supports configurable retention policies that automatically purge old Audit Logs, Connection Logs, Workspace Agent Logs, API keys, and AI Gateway records.
These policies help manage database growth by removing records older than a specified duration.
2025-12-02 17:47:36 +02:00
## Overview
Large deployments can accumulate significant amounts of data over time.
Retention policies help you:
2026-10-14 14:29:20 -04:00
- **Reduce database size**:
Automatically remove old records to free disk space.
- **Improve performance**:
Smaller tables mean faster queries and backups.
- **Meet compliance requirements**:
Configure retention periods that align with your organization's data retention policies.
2025-12-02 17:47:36 +02:00
> [!NOTE]
2026-09-28 12:29:40 -04:00
> Retention policies are disabled by default (set to `0`) to preserve existing behavior.
2026-11-13 16:34:59 -05:00
> The exceptions are API keys and workspace agent logs, which default to 7 days, and AI Gateway records, which default to 60 days.
2025-12-02 17:47:36 +02:00
## Configuration
2026-10-14 14:29:20 -04:00
You can configure retention policies using CLI flags, environment variables, or a YAML configuration file.
2025-12-02 17:47:36 +02:00
### Settings
2025-12-02 18:01:33 +02:00
| Setting | CLI Flag | Environment Variable | Default | Description |
|----------------------|------------------------------------|----------------------------------------|----------------|-----------------------------------------|
| Audit Logs | `--audit-logs-retention` | `CODER_AUDIT_LOGS_RETENTION` | `0` (disabled) | How long to retain Audit Log entries |
| Connection Logs | `--connection-logs-retention` | `CODER_CONNECTION_LOGS_RETENTION` | `0` (disabled) | How long to retain Connection Logs |
| API Keys | `--api-keys-retention` | `CODER_API_KEYS_RETENTION` | `7d` | How long to retain expired API keys |
| Workspace Agent Logs | `--workspace-agent-logs-retention` | `CODER_WORKSPACE_AGENT_LOGS_RETENTION` | `7d` | How long to retain workspace agent logs |
2026-05-21 11:14:11 +02:00
| AI Gateway | `--ai-gateway-retention` | `CODER_AI_GATEWAY_RETENTION` | `60d` | How long to retain AI Gateway records |
2025-12-03 11:39:36 +02:00
> [!NOTE]
2026-04-09 12:07:50 +02:00
> AI Gateway retention is configured separately from other retention settings.
2026-11-04 17:15:42 -05:00
> See [AI Gateway Setup](../../ai-governance/ai-gateway/setup.md#data-retention) for detailed configuration options.
2025-12-02 17:47:36 +02:00
### Duration Format
2026-10-14 14:29:20 -04:00
Retention durations support days (`d` ) and weeks (`w` ) in addition to standard Go duration units (`h` , `m` , `s` ):
2025-12-02 17:47:36 +02:00
2026-09-28 12:29:40 -04:00
- `7d` - 7 days
- `2w` - 2 weeks
- `30d` - 30 days
- `90d` - 90 days
- `365d` - 1 year
2025-12-02 17:47:36 +02:00
### CLI Example
2026-07-15 14:07:09 -04:00
```sh
2025-12-02 17:47:36 +02:00
coder server \
--audit-logs-retention= 365d \
--connection-logs-retention= 90d \
2025-12-02 18:01:33 +02:00
--api-keys-retention= 7d \
2025-12-03 11:39:36 +02:00
--workspace-agent-logs-retention= 7d \
2026-05-21 11:14:11 +02:00
--ai-gateway-retention= 60d
2025-12-02 17:47:36 +02:00
```
### Environment Variables Example
2026-07-15 14:07:09 -04:00
```sh
2025-12-02 17:47:36 +02:00
export CODER_AUDIT_LOGS_RETENTION = 365d
export CODER_CONNECTION_LOGS_RETENTION = 90d
export CODER_API_KEYS_RETENTION = 7d
2025-12-02 18:01:33 +02:00
export CODER_WORKSPACE_AGENT_LOGS_RETENTION = 7d
2026-05-21 11:14:11 +02:00
export CODER_AI_GATEWAY_RETENTION = 60d
2025-12-02 17:47:36 +02:00
```
### YAML Configuration Example
```yaml
retention :
audit_logs : 365d
connection_logs : 90d
api_keys : 7d
2025-12-02 18:01:33 +02:00
workspace_agent_logs : 7d
2025-12-03 11:39:36 +02:00
2026-05-21 11:14:11 +02:00
ai_gateway :
2025-12-03 11:39:36 +02:00
retention : 60d
2025-12-02 17:47:36 +02:00
```
## How Retention Works
### Background Purge Process
2026-10-14 14:29:20 -04:00
Coder runs a background process that periodically deletes old records.
The purge process:
2025-12-02 17:47:36 +02:00
2026-09-28 12:29:40 -04:00
1. Runs approximately every 10 minutes.
2025-12-02 17:47:36 +02:00
2. Processes records in batches to avoid database lock contention.
3. Deletes records older than the configured retention period.
4. Logs the number of deleted records for monitoring.
### Effective Retention
Each retention setting controls its data type independently:
- When set to a non-zero duration, records older than that duration are deleted.
- When set to `0` , retention is disabled and data is kept indefinitely.
### API Keys Special Behavior
2026-10-14 14:29:20 -04:00
API key retention only affects **expired** keys.
A key is deleted only when:
2025-12-02 17:47:36 +02:00
1. The key has expired (past its `expires_at` timestamp).
2. The key has been expired for longer than the retention period.
2026-09-28 12:29:40 -04:00
Setting `--api-keys-retention=7d` deletes keys that expired more than 7 days ago.
Active keys are never deleted by the retention policy.
2025-12-02 17:47:36 +02:00
2026-10-14 14:29:20 -04:00
Keeping expired keys for a short period allows Coder to return a more helpful error message when users attempt to use an expired key.
2025-12-02 17:47:36 +02:00
2025-12-02 18:01:33 +02:00
### Workspace Agent Logs Behavior
2026-10-14 14:29:20 -04:00
Workspace agent logs are deleted based on when the agent last connected, not the age of the logs themselves.
**Logs from the latest build of each workspace are always retained** regardless of when the agent last connected.
2026-11-06 22:26:01 +00:00
This makes sure you can always debug issues with active workspaces.
2025-12-02 18:01:33 +02:00
2026-09-28 12:29:40 -04:00
For non-latest builds, logs are deleted if the agent hasn't connected within the retention period.
Setting `--workspace-agent-logs-retention=7d` deletes logs for agents that haven't connected in 7 days (excluding those from the latest build).
2025-12-02 18:01:33 +02:00
2026-04-09 12:07:50 +02:00
### AI Gateway Data Behavior
2025-12-03 11:39:36 +02:00
2026-09-28 12:29:40 -04:00
AI Gateway retention applies to interception records and all related data, including token usage, prompts, and tool invocations.
The default of 60 days provides a reasonable balance between storage costs and the ability to analyze usage patterns.
2025-12-03 11:39:36 +02:00
2026-11-04 17:15:42 -05:00
For details on what data is retained, see the [AI Gateway Data Retention ](../../ai-governance/ai-gateway/setup.md#data-retention ) documentation.
2025-12-03 11:39:36 +02:00
2025-12-02 17:47:36 +02:00
## Best Practices
### Recommended Starting Configuration
For most deployments, we recommend:
```yaml
retention :
audit_logs : 365d
connection_logs : 90d
api_keys : 7d
2025-12-02 18:01:33 +02:00
workspace_agent_logs : 7d
2025-12-03 11:39:36 +02:00
2026-05-21 11:14:11 +02:00
ai_gateway :
2025-12-03 11:39:36 +02:00
retention : 60d
2025-12-02 17:47:36 +02:00
```
### Compliance Considerations
> [!WARNING]
2026-10-14 14:29:20 -04:00
> Audit Logs provide critical security and compliance information.
2026-11-06 17:17:35 -05:00
> Purging Audit Logs might impact your organization's ability to investigate security incidents or meet compliance requirements.
2026-10-14 14:29:20 -04:00
> Consult your security and compliance teams before configuring Audit Log retention.
2025-12-02 17:47:36 +02:00
2026-11-12 13:20:32 -05:00
Retention requirements depend on the regulations and frameworks your organization follows, and on how your organization applies them.
Some frameworks set a minimum period for audit records, and others don't prescribe one, so Coder doesn't recommend a period for any specific framework.
2025-12-02 17:47:36 +02:00
### External Log Aggregation
2026-10-23 22:27:18 -07:00
If you use an external log aggregation system (Splunk, Datadog, and so on), you can configure shorter retention periods in Coder since logs are preserved externally.
2026-10-14 14:29:20 -04:00
See [Capture and export audit logs ](../security/audit-logs.md#capture-and-export-audit-logs ) for details on exporting logs.
2025-12-02 17:47:36 +02:00
### Database Maintenance
2026-11-06 17:17:35 -05:00
After enabling retention policies, you might want to run a `VACUUM` operation on your PostgreSQL database to reclaim disk space.
2026-10-14 14:29:20 -04:00
See [Maintenance Procedures ](../security/audit-logs.md#maintenance-procedures-for-the-audit-logs-table ) for guidance.
2025-12-02 17:47:36 +02:00
2026-09-28 13:40:07 -04:00
<a id="keeping-data-indefinitely"></a>
## Keep data indefinitely
2025-12-02 17:47:36 +02:00
To keep data indefinitely for any data type, set its retention value to `0` :
```yaml
retention :
2025-12-02 18:01:33 +02:00
audit_logs : 0s # Keep audit logs forever
connection_logs : 0s # Keep connection logs forever
api_keys : 0s # Keep expired API keys forever
workspace_agent_logs : 0s # Keep workspace agent logs forever
2025-12-03 11:39:36 +02:00
2026-05-21 11:14:11 +02:00
ai_gateway :
2026-04-09 12:07:50 +02:00
retention : 0s # Keep AI Gateway records forever
2025-12-02 17:47:36 +02:00
```
## Monitoring
2026-10-14 14:29:20 -04:00
The purge process logs deletion counts at the `DEBUG` level.
2026-10-23 22:27:18 -07:00
To monitor retention activity, enable debug logging or search your logs for entries containing the table name (for example, `audit_logs` , `connection_logs` , `api_keys` ).
2025-12-02 17:47:36 +02:00
## Related Documentation
2026-10-14 14:29:20 -04:00
- [Audit Logs ](../security/audit-logs.md ): Learn about Audit Logs and manual purge procedures.
- [Connection Logs ](../monitoring/connection-logs.md ): Learn about Connection Logs and monitoring.
2026-11-04 17:15:42 -05:00
- [AI Gateway ](../../ai-governance/ai-gateway/index.md ): Learn about AI Gateway for centralized large language model (LLM) and Model Context Protocol (MCP) proxy management.
- [AI Gateway Setup ](../../ai-governance/ai-gateway/setup.md#data-retention ): Configure AI Gateway data retention.
- [AI Gateway Monitoring ](../../ai-governance/ai-gateway/monitoring.md ): Monitor AI Gateway usage and metrics.