Can i cancel a flow run using rest api <@ULVA73B9P...
# ask-marvin
a
Can i cancel a flow run using rest api @Marvin
m
βœ… thought for 162.3 seconds
n
yes, tldr call
set_flow_run_state
to
Cancelled
with
force=True
, we'll see if marvin gets it
πŸ™Œ 1
m
Yes. In Prefect 3.x you can cancel a flow run by setting its state to CANCELLING via the REST API. What to call - Endpoint: POST /flow_runs/{flow_run_id}/set_state - Body:
Copy code
{
  "state": {
    "type": "CANCELLING",
    "message": "User requested cancellation"
  }
}
- Notes: - CANCELLING signals the run to stop; it will move to CANCELLED once the worker acknowledges. - If orchestration blocks the transition, add ?force=true to the URL. Use sparingly. Curl examples - Prefect Cloud:
Copy code
curl -X POST \
  "<https://api.prefect.cloud/api/accounts/<ACCOUNT_ID>/workspaces/<WORKSPACE_ID>/flow_runs/<FLOW_RUN_ID>/set_state>" \
  -H "Authorization: Bearer <PREFECT_API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "state": { "type": "CANCELLING", "message": "User requested cancellation" }
  }'
- Self-hosted server:
Copy code
curl -X POST \
  "http://<PREFECT_SERVER_URL>/api/flow_runs/<FLOW_RUN_ID>/set_state" \
  -H "Content-Type: application/json" \
  -d '{
    "state": { "type": "CANCELLING", "message": "User requested cancellation" }
  }'
To verify it’s done, poll the run: - GET /flow_runs/{flow_run_id} until you see state.type = CANCELLED. Docs, for your reading pleasure (if you must): - Cancel workflows - API client basics Want me to tailor this for your setup (Cloud vs self-hosted) or wire up a quick script?
n
ah yea my suggestion would be to force it into
Cancelled
unequivocally but marvin's is correct if you have
on_cancellation
hooks you'd want to run etc
πŸ™Œ 1