Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:PrefectHQ Prefect Client Delete Flow Run

From Leeroopedia
Revision as of 16:22, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/PrefectHQ_Prefect_Client_Delete_Flow_Run.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

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