<@ULVA73B9P> I have a self hosted Prefect 3 server...
# ask-marvin
n
@Marvin I have a self hosted Prefect 3 server. I have an automation which triggers whenever a flow stays in the Cancelling state for at least one hour: in this case, the actions is to cancel the flow. The problem is that this does not happen: I have flows in the cancelling state that have been in that state for days. Also, the Prefect server shows this log from time to time:
Copy code
prefect.server.events.actions - Action failed: 'Failed to set state: Cannot transition flows that are cancelling to a state other than Cancelled.'
Why?
m
thought for 726.8 seconds
Short answer: your automation is using the wrong action for runs that are already in Cancelling. - The “Cancel flow run” action initiates a cancellation request that transitions a run into the Cancelling state (it’s the right action when a run is Running/Pending). - Once a run is already in Cancelling, Prefect’s orchestration rules only allow it to move to Cancelled. Any attempt to “cancel” again (i.e., set it to Cancelling) is rejected. That’s why you see:
Copy code
Action failed: 'Failed to set state: Cannot transition flows that are cancelling to a state other than Cancelled.'
What to change - Update your automation so that when a run has been in Cancelling for ≥ 1 hour, the action is “Set flow run state” and you choose “Cancelled”. - Do not use “Cancel flow run” for runs already in Cancelling. Why this happens - Cancelling is a non-terminal “cleanup in progress” state. The only valid next state is Cancelled. Reissuing a cancel (which sets Cancelling) from Cancelling is rejected by the server’s orchestration rules. Relevant docs - States in Prefect 3.x: States - Cancellation workflow: Cancel workflows - Flow run set-state API (shows the “Cancelled” terminal state): Set flow run state - Orchestration/core policy and rules (the rule behind that error): Core policy and Rules If you want, I can help double-check your automation config—could you share a screenshot or describe the exact action you selected?
n
@Marvin is there an automation action to delete flow runs?
m
thought for 331.9 seconds
Short answer: no. There isn’t a built‑in automation action that deletes flow runs. What you can use instead - CLI:
prefect flow-run delete <flow-run-id>
(this command exists in Prefect 3.x; run
prefect flow-run delete --help
to confirm) - REST API: DELETE endpoint DELETE /flow-runs/{id} - Python client:
Copy code
from uuid import UUID
from prefect.client.orchestration import get_client

async def delete_run(run_id: str):
    async with get_client() as client:
        await client.delete_flow_run(UUID(run_id))
If you want it “automated” - Use an Automation that triggers on your condition (e.g., Cancelling for ≥ 1h) and then: - Run a “cleanup” deployment (action: Run deployment) that calls the API to delete matching runs, or - Call a webhook (action: Call webhook) that performs the deletion via the REST API. Notes - Deleting a run is destructive: it removes the run record (and associated logs/artifacts). It does not stop any underlying infrastructure. If the run is actually still executing somewhere, cancel/clean up that infra first. - There’s no built‑in retention policy for flow runs in Prefect 3; if you need periodic cleanup, schedule a small maintenance flow to prune old runs. For general maintenance guidance, see Database maintenance.