prefect.server.services.db_vacuum
The database vacuum service. Three perpetual services schedule cleanup tasks
independently, gated by the enabled set in
PREFECT_SERVER_SERVICES_DB_VACUUM_ENABLED (default ["events"]):
-
schedule_vacuum_tasks — Cleans up old flow runs, then deletes their logs
and artifacts by
flow_run_idin bounded batches. Enabled when"flow_runs"is in the enabled set. -
schedule_event_vacuum_tasks — Cleans up old events, including any
event types with per-type retention overrides. Enabled when
"events"is in the enabled set andevent_persister.enabledis true (the default), so that operators who disabled event processing are not surprised on upgrade. Runs in all server modes, including ephemeral. -
schedule_orphan_vacuum_tasks — Cleans up logs and artifacts orphaned by
interrupted cleanup, late writes, direct SQL deletion, or older Prefect
versions. It runs daily when flow run vacuum is enabled, rather than on the
hourly vacuum loop, and can also be enabled independently with
"orphans".
PREFECT_SERVER_SERVICES_DB_VACUUM_EVENT_RETENTION_OVERRIDES. Event types
not listed fall back to server.events.retention_period.
Each task runs independently with its own error isolation and
docket-managed retries. Deterministic keys prevent duplicate tasks from
accumulating if a cycle overlaps with in-progress work.
Functions
schedule_vacuum_tasks
schedule_event_vacuum_tasks
"events" is in the default enabled set).
Automatically disabled when the event persister service is disabled
(PREFECT_SERVER_SERVICES_EVENT_PERSISTER_ENABLED=false) so that
operators who opted out of event processing are not surprised by
trimming on upgrade.
schedule_orphan_vacuum_tasks
vacuum_orphaned_logs
vacuum_orphaned_artifacts
vacuum_stale_artifact_collections
vacuum_old_flow_runs
log and artifact have no ON DELETE CASCADE from flow_run. Each run
batch is selected and deleted in one locking transaction, then its children
are deleted by flow_run_id using indexed lookups.
batch_size bounds how many runs are selected, not how many children they
have, so children are deleted in separately committed batches rather than
in one transaction with the runs and their cascaded task runs and states.
Deleting the runs first prevents a concurrent state transition from
preserving a run after some of its children were permanently deleted. If
child cleanup is interrupted, orphan reconciliation recovers it.
vacuum_events_with_retention_overrides
event_retention_overrides and deletes
events (and their resources) that are older than the configured retention
for that type, capped by the global events retention period.