# Set how long metrics and logs are kept

Read and change your organization's retention policy for built-in metrics and logs, and see how much log archive is stored.

Your organization keeps built-in metrics for 90 days and logs for 30 days unless you set less. Each
is kept in two tiers:

| Data    | First tier                                                    | Second tier                                                                                      |
| ------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| Metrics | One point per minute for 14 days                              | One point per hour (average, sum, minimum, maximum and count, all exact) up to the policy's days |
| Logs    | Searchable at once for 7 days (or the policy's days if fewer) | An archive in object storage, still searched by **Logs**, up to the policy's days                |

The policy covers the metrics of machines, databases, VPN connections, Kubernetes clusters, CDN
distributions and apps, and every log line Monitoring holds. Custom metrics (15 days per minute, 15
months per hour) and uptime results (30 days) keep their own retention.

## Prerequisites

- `monitoring:GetRetentionPolicy` to read the policy and `monitoring:PutRetentionPolicy` to change
  it, on `rn:ews::<organization id>:monitoring:*`.

## Change the policy

**Console**
1. Open **Monitoring**. The **Retention** card shows both numbers and how much log archive is
   stored.
2. Choose **Change**, enter the days for **Metrics** (14–90) and **Logs** (1–30), and choose
   **Save**. When you lower a number, the dialog says what the next daily sweep deletes before you
   save.

**CLI**
```sh
dcloud monitoring retention-policy get
dcloud monitoring retention-policy put --metrics-retention-days 30 --logs-retention-days 7
```

**API**
```http
PUT /monitoring/api/retention-policy
Content-Type: application/json

{ "metricsRetentionDays": 30, "logsRetentionDays": 7 }
```

**Terraform**
```hcl
resource "dcloud_monitoring_retention_policy" "this" {
  metrics_retention_days = 30
  logs_retention_days    = 7
}
```

Destroying the resource sets both values back to their defaults (90 and 30 days).

Only the fields you send change.

## Result

The answer is the policy: `metricsRetentionDays`, `logsRetentionDays`, the limits
(`minMetricsRetentionDays` … `maxLogsRetentionDays`) and `logArchive`, which counts the archived
files, their lines, their size as written (`rawBytes`) and as stored compressed (`storedBytes`).
When you lowered a number, `warning` says what the next daily sweep deletes and when it runs.

## Things to know

- **Lowering deletes.** At the next daily sweep (03:20 UTC), metrics and logs older than the new
  limit are deleted for good. There is no grace period; raise the number again before the sweep to
  keep them.
- **Logs are kept by whole UTC days.** 1 day keeps today and yesterday, so a line is gone at most
  two days after it was written. With fewer than 7 days, nothing is archived.
- **Charts over the hourly tier** show one point per hour, even with a shorter period.
- **Searching the archive** reads at most 62 archived files or 512 MiB in one search. The search
  answer says what it read in `archive` (`objectsScanned`, `linesScanned`, `truncated`); narrow the
  range when `truncated` is `true`.
- **Longer retention** (metrics up to 15 months, logs up to 3 years) is planned as a paid option and
  cannot be set yet: a value above 90 or 30 days answers 400.

## Related

- [Read metrics](read-metrics.md)
- [Search logs](search-logs.md)
- [Quotas and limits](quotas.md)
