Implementation:PrefectHQ Prefect Client Delete Flow Run
Appearance
| Metadata | |
|---|---|
| Sources | Prefect |
| Domains | Orchestration, Database_Maintenance |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Concrete async client method for deleting flow run records in rate-limited batches provided by the Prefect library.
Description
The client.delete_flow_run method deletes a single flow run by UUID. In the database cleanup workflow, it is called in a batched loop with asyncio.sleep(0.1) for rate limiting and ObjectNotFound exception handling for concurrent cleanup safety.
Code Reference
- Repository: https://github.com/PrefectHQ/prefect
- File: examples/ai_database_cleanup_with_approval.py (L244-257)
- Signature:
deleted = 0
async with get_client() as client:
for i in range(0, len(flow_runs), config.batch_size):
batch = flow_runs[i : i + config.batch_size]
for run in batch:
try:
await client.delete_flow_run(run.id)
deleted += 1
except ObjectNotFound:
deleted += 1 # Already deleted
except Exception as e:
print(f"failed to delete {run.id}: {e}")
await asyncio.sleep(0.1) # rate limiting
- Import:
from prefect import get_client;from prefect.exceptions import ObjectNotFound;import asyncio
I/O Contract
Inputs
- run.id (UUID, required) — flow run ID to delete
- config.batch_size (int) — batch size for loop
Outputs
- dict with status ("completed") and deleted count
Usage Example
import asyncio
from prefect import get_client
from prefect.exceptions import ObjectNotFound
async def delete_flow_runs(flow_runs, batch_size=100):
deleted = 0
async with get_client() as client:
for i in range(0, len(flow_runs), batch_size):
batch = flow_runs[i : i + batch_size]
for run in batch:
try:
await client.delete_flow_run(run.id)
deleted += 1
except ObjectNotFound:
deleted += 1
await asyncio.sleep(0.1)
return {"status": "completed", "deleted": deleted}
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment