Jakub Sosnovec
09/09/2024, 2:04 PMPREFECT_EVENTS_RETENTION_PERIOD
setting, but it seems it's only running if PREFECT_EXPERIMENTAL_EVENTS
and even in that case I don't know what it's actually is supposed to do.
We are finding that with many flows, our database is getting quite full, especially if the flow run logs are big.
So, is there some setting which would make self-hosted Prefect server delete old flow runs from the database?Marvin
09/09/2024, 2:04 PMJakub Sosnovec
09/09/2024, 2:04 PMJakub Sosnovec
09/09/2024, 2:12 PMMarvin
09/09/2024, 2:13 PMpython
from prefect import get_client
from datetime import datetime, timedelta
import asyncio
async def delete_old_flow_runs(days_old: int):
async with get_client() as client:
cutoff_date = datetime.utcnow() - timedelta(days=days_old)
flow_runs = await client.read_flow_runs(
filter=dict(
state=dict(type=["Completed", "Failed", "Canceled"]),
end_time=dict(before=cutoff_date)
)
)
for flow_run in flow_runs:
print(f"Deleting flow run {flow_run.id}")
await client.delete_flow_run(flow_run.id)
if __name__ == "__main__":
asyncio.run(delete_old_flow_runs(30)) # Replace 30 with the number of days
Links to confirmed info:
- Flow Run Retention Policy in Prefect Cloud
- GitHub Issue on Removing Flow Run Records
Hope this helps!