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
knowledgeandprerequisites: 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 doctorpatterns for your inputs
3. Act — execute with data-retention tools
- Tools:
Glob,Grep,Read,Aws,Mongosh(see frontmattertools/allowed-tools) - Use
safe_pathfor 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
{
"Rules": [
{
"ID": "expire-raw-events",
"Status": "Enabled",
"Filter": {"Prefix": "raw/"},
"Transitions": [
{"Days": 30, "StorageClass": "STANDARD_IA"},
{"Days": 90, "StorageClass": "GLACIER"}
],
"Expiration": {"Days": 365}
}
]
}
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
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
}
logrotate -d /etc/logrotate.d/api
logrotate -f /etc/logrotate.d/api
Testing
# 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 namedays(string): Expiration or transition days
Commands:
aws s3api put-bucket-lifecycle-configuration --bucket my-bucket --lifecycle-configuration file://lifecycle.jsonaws s3api get-bucket-lifecycle-configuration --bucket my-bucketaws s3api delete-bucket-lifecycle --bucket my-bucketaws 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 daycollection(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/apilogrotate -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