Devih
09/15/2025, 8:04 AMMarvin
09/15/2025, 8:04 AMMarvin
09/15/2025, 8:09 AMevent_resources data. In Prefect 3.x this is generally handled by pruning old events; the associated event_resources rows are removed with them via foreign keys. Here are docs and a safe approach.
Useful docs
- Database maintenance (retention, pruning, and Postgres tips): Database maintenance
- Events concepts (what gets stored and why): Events concepts
- Event persister API (batch deletion helper used by the server): Event persister API
- Settings reference (events retention setting): Settings reference
Options to clean up
1) Configure automatic retention (recommended)
- Set an environment variable on your Prefect Server to control how long events are kept:
- PREFECT_SERVER_EVENTS_RETENTION_PERIOD (default is 7 days)
- After you set this and restart the server, old events (and their related event_resources) will be pruned over time.
2) One-time manual cleanup in Postgres
- Preview what will be deleted:
SELECT e.id, e.occurred
FROM events e
WHERE e.occurred < now() - interval '30 days'
ORDER BY e.id
LIMIT 1000;
- Delete in batches to avoid long locks (this will also delete from event_resources if there’s ON DELETE CASCADE from events):
DELETE FROM events
WHERE id IN (
SELECT id FROM events
WHERE occurred < now() - interval '30 days'
ORDER BY id
LIMIT 10000
);
- If your schema does not have ON DELETE CASCADE, delete from event_resources first:
DELETE FROM event_resources er
WHERE er.event_id IN (
SELECT id FROM events
WHERE occurred < now() - interval '30 days'
ORDER BY id
LIMIT 10000
);
- Run the batch DELETE repeatedly until it affects 0 rows.
- After large deletions, use Postgres maintenance to reclaim space (e.g., pg_repack is generally preferred over VACUUM FULL for minimal locking).
Questions to tailor guidance
- Are you running Prefect Cloud or self-hosted Prefect Server?
- What database/version are you using (Postgres assumed)?
- How much history do you want to keep (e.g., 7, 30, 90 days)?
- Do you know if your events → event_resources foreign key has ON DELETE CASCADE?
If you share those details, I can suggest exact SQL and steps for your environment.