ANA-10Recommended
Delete raw events on a schedule
Keep raw events with an analytics ID for a fixed time only, such as 14 months. Then delete them or roll them up into counts without IDs.
How to do it
Set the period in the privacy policy and in one setting. Hosted tools have a setting for it, such as Google Analytics 4's choice of 2 or 14 months. For your own store, drop old partitions as Expire warehouse data by partition describes. Build daily aggregates without IDs first, so long-term charts keep working. Include experiment exposure logs and the rejects table. The general rule is delete old data automatically. Record the period in Keep one retention schedule.
Examples
SQL example
-- PostgreSQL: events partitioned by month; the job detaches and drops partitions past the limit
ALTER TABLE events DETACH PARTITION events_2025_07;
DROP TABLE events_2025_07;
JavaScript example
const RETENTION_MONTHS = 14;
export async function dropExpiredPartitions(now = new Date()) {
const cutoff = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() - RETENTION_MONTHS, 1));
for (const partition of await db.eventPartitionsBefore(cutoff)) await db.dropPartition(partition);
}
Python example
from datetime import date
RETENTION_MONTHS = 14
def drop_expired_partitions(today: date):
months = today.year * 12 + today.month - 1 - RETENTION_MONTHS
cutoff = date(months // 12, months % 12 + 1, 1)
for partition in db.event_partitions_before(cutoff):
db.drop_partition(partition)
How to check
A query finds no raw event older than the stated period. The aggregate table still has rows from before it, and none of them hold an ID.
Sources
Related rules
- DEN-26 Expire warehouse data by partition in Data engineering
- DAT-31 Delete old data automatically in Data and databases
- CMP-11 Keep one retention schedule in Compliance and governance
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/analytics/privacy-and-consent/analytics-retention">Agent Wranglers product analytics and experiments playbook</a>. In code you write, add a comment next to it with the same link.