# Data Retention

> Expires, archives, and rotates API data with S3 lifecycle policies, MongoDB TTL indexes, and log rotation configs for compliance and cost control. Use when working with s3 lifecycle, mongo ttl, api or when the user mentions s3 lifecycle, mongo ttl, api.

- Skill: `opknock/data-retention` (Agent Skill)
- Install (CLI): `npx get-skill add opknock/data-retention`
- Raw SKILL.md: https://api.skillmd.com/api/skills/opknock/data-retention/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- License: MIT
- Author: OpKnock (https://skillmd.com/u/opknock)
- Updated: 2026-11-23
- Page: https://skillmd.com/skills/opknock/data-retention

---


Expires, archives, and rotates API data with S3 lifecycle policies, MongoDB TTL indexes, and log rotation configs for compliance and cost control.

## Agentic Workflow: Read -> Reason -> Act (data-retention)

You are **Data Retention** (api/general) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.

### 1. Read — api context for `data-retention`
- Domain: Expires, archives, and rotates API data with S3 lifecycle policies, MongoDB TTL indexes, and log rotation configs for compliance and cost control.
- **s3-lifecycle**: Configure S3 bucket lifecycle policies for tiering and expiring objects — `aws s3api put-bucket-lifecycle-configuration --bucket my-bucket --lifecycle-conf`
- **mongo-ttl**: Use MongoDB TTL indexes and logrotate to expire API data and logs — `mongosh --quiet --eval 'db.events.createIndex({createdAt:1},{expireAfterSeconds:`
- Check `knowledge` and `prerequisites: aws, logrotate, mongosh`

### 2. Reason — think for `data-retention`
- For `s3-lifecycle`: Configure S3 bucket lifecycle policies for tiering and expiring objects — decide which checks to run
- For `mongo-ttl`: Use MongoDB TTL indexes and logrotate to expire API data and logs — decide which checks to run
- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs

### 3. Act — execute with `data-retention` tools
- Tools: `Glob`, `Grep`, `Read`, `Aws`, `Mongosh` (see frontmatter `tools`/`allowed-tools`)
- Use `safe_path` for any write; record evidence (paths, checksums)
- Fingerprint: `data-retention:249123b7`

# Data Retention

Expire, archive, and rotate API data with retention policies.

## When to Use

- GDPR/CCPA compliance for stored API data
- Controlling storage costs for logs and events
- Enforcing retention windows per data class

## S3 Lifecycle

```json
{
  "Rules": [
    {
      "ID": "expire-raw-events",
      "Status": "Enabled",
      "Filter": {"Prefix": "raw/"},
      "Transitions": [
        {"Days": 30, "StorageClass": "STANDARD_IA"},
        {"Days": 90, "StorageClass": "GLACIER"}
      ],
      "Expiration": {"Days": 365}
    }
  ]
}
```

```bash
aws s3api put-bucket-lifecycle-configuration --bucket my-bucket \
  --lifecycle-configuration file://lifecycle.json
aws s3api get-bucket-lifecycle-configuration --bucket my-bucket
```

## MongoDB TTL Index

```bash
mongosh --quiet --eval 'db.events.createIndex({createdAt:1},{expireAfterSeconds:86400})'
mongosh --quiet --eval 'db.events.getIndexes()'
```

TTL deletes documents where createdAt is older than expireAfterSeconds. Requires an indexed date field.

## Log Rotation

```
/etc/logrotate.d/api:
/var/log/api/*.log {
  daily
  rotate 30
  compress
  missingok
  notifempty
}
```

```bash
logrotate -d /etc/logrotate.d/api
logrotate -f /etc/logrotate.d/api
```

## Testing

```bash
# Verify lifecycle config is active
aws s3api get-bucket-lifecycle-configuration --bucket my-bucket | jq '.Rules'
# Inspect TTL index options
mongosh --quiet --eval 'db.events.getIndexes()' | jq '.[] | select(.expireAfterSeconds)'
```

## Best Practices

- Define retention per data class (raw, processed, logs)
- Test TTL with small expireAfterSeconds values first
- Use transition before expiration to save costs
- Document retention in a data policy
- Monitor delete and transition metrics
- Prefer encryption and lifecycle over manual deletion

## Capabilities

### s3-lifecycle
Configure S3 bucket lifecycle policies for tiering and expiring objects

**Parameters:**
- `bucket` (string): S3 bucket name
- `days` (string): Expiration or transition days

**Commands:**
- `aws s3api put-bucket-lifecycle-configuration --bucket my-bucket --lifecycle-configuration file://lifecycle.json`
- `aws s3api get-bucket-lifecycle-configuration --bucket my-bucket`
- `aws s3api delete-bucket-lifecycle --bucket my-bucket`
- `aws s3 ls s3://my-bucket --recursive | wc -l`

**Examples:**
- aws s3api put-bucket-lifecycle-configuration --bucket my-bucket --lifecycle-configuration file://lifecycle.json
- aws s3api get-bucket-lifecycle-configuration --bucket my-bucket | jq '.Rules'
- aws s3 ls s3://my-bucket --recursive --summarize | tail -3

### mongo-ttl
Use MongoDB TTL indexes and logrotate to expire API data and logs

**Parameters:**
- `expire_seconds` (string): TTL in seconds, e.g. 86400 for one day
- `collection` (string): MongoDB collection to expire

**Commands:**
- `mongosh --quiet --eval 'db.events.createIndex({createdAt:1},{expireAfterSeconds:86400})'`
- `mongosh --quiet --eval 'db.events.getIndexes()'`
- `logrotate -d /etc/logrotate.d/api`
- `logrotate -f /etc/logrotate.d/api`

**Examples:**
- mongosh --quiet --eval 'db.events.createIndex({createdAt:1},{expireAfterSeconds:2592000})'
- mongosh --quiet --eval 'db.events.getIndexes()' | jq
- logrotate -d /etc/logrotate.d/api

## References
- [S3 Lifecycle Docs](https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lifecycle-mgmt.html)
- [MongoDB TTL Indexes](https://www.mongodb.com/docs/manual/core/index-ttl/)

