<@ULVA73B9P> I've queued multiple runs on kubernet...
# marvin-ai
j
@Marvin I've queued multiple runs on kubernetes, they are all backed up and in pending state. How to cancel them on bulk? Does deleting the deployment, kill the pending runs? Does restarting the working kill the runs?
m
thought for 370.1 seconds
Short answer: - Bulk-cancel: there isn’t a single “cancel all” button, but you can cancel in bulk via a short script (Python or a shell loop). - Deleting the deployment: does NOT cancel existing flow runs (pending or otherwise). - Restarting the worker: does NOT cancel existing flow runs; pending runs remain pending and will be picked up again when the worker is back. Ways to cancel pending runs in bulk Option A — Python (most reliable) - Cancels all PENDING (and/or SCHEDULED) runs, optionally scoped to a specific deployment.
Copy code
import 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.
Copy code
# 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:
Copy code
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.