Jashaul
10/09/2025, 10:20 AMMarvin
10/09/2025, 10:20 AMMarvin
10/09/2025, 10:27 AMimport asyncio
from prefect.client.orchestration import get_client
from prefect.client.schemas.filters import FlowRunFilter, FlowRunFilterStateType, FlowRunFilterDeploymentId
from prefect.client.schemas.objects import StateType
from prefect.states import Cancelled
DEPLOYMENT_ID = None # or set to a specific deployment UUID string
async def main():
async with get_client() as client:
fr_filter = FlowRunFilter(
state_type=FlowRunFilterStateType(any_=[StateType.PENDING, StateType.SCHEDULED])
)
if DEPLOYMENT_ID:
fr_filter.deployment_id = FlowRunFilterDeploymentId(any_=[DEPLOYMENT_ID])
runs = await client.read_flow_runs(flow_run_filter=fr_filter, limit=1000)
for run in runs:
await client.set_flow_run_state(run.id, Cancelled(name="Bulk cancel"))
asyncio.run(main())
Docs:
- Cancel workflows in Prefect 3
Option B — CLI loop (quick and dirty)
- List pending runs, grab IDs, and cancel them. You can narrow by flow name with --flow-name.
- Note: output formatting can vary; test on a few runs first.
# List up to 1000 pending runs, extract IDs, cancel each
prefect flow-run ls --state PENDING --limit 1000 \
| awk 'NR>2 {print $1}' \
| xargs -n1 prefect flow-run cancel
CLI refs:
- prefect flow-run ls --help (shows filtering by state/flow name)
- prefect flow-run cancel --help
Pausing work to stop new pickups (optional)
- If you need to stop workers from pulling more runs while you clean up:
prefect work-queue pause <queue-name>
# later...
prefect work-queue resume <queue-name>
What happens if…
- You delete the deployment: existing runs are unaffected; they will not be auto-cancelled. You must cancel them explicitly.
- You restart the worker (Kubernetes worker): does not cancel pending runs. Already-launched Kubernetes pods keep running; runs that haven’t launched a pod yet remain pending and will be picked up again when the worker is back.
If you share how you want to scope the cancellations (all pending, only for a deployment, only for a flow name, only older than X minutes), I can tailor the exact script/command.