<@ULVA73B9P> how to cancel a runnning flow run via...
# marvin-ai
l
@Marvin how to cancel a runnning flow run via rest api
m
thought for 453.9 seconds
Assuming Prefect 3.x: you cancel a running flow run by setting its state via the REST API. Two options: - Graceful cancel: set state to CANCELLING (lets Prefect clean up infrastructure, then it moves the run to CANCELLED) - Force cancel: set state to CANCELLED with ?force=true to bypass orchestration rules Key endpoint: - Server (self-hosted): POST /api/flow_runs/{flow_run_id}/set_state - Cloud: POST https://api.prefect.cloud/api/accounts/{account_id}/workspaces/{workspace_id}/flow_runs/{flow_run_id}/set_state Auth: - Prefect Cloud: add Authorization: Bearer YOUR_API_KEY - Self-hosted Prefect Server: usually no auth by default; if you’ve enabled API auth, include the configured auth header Examples 1) Prefect Cloud – graceful cancel (recommended)
Copy code
ACCOUNT_ID=<your_account_id>
WORKSPACE_ID=<your_workspace_id>
FLOW_RUN_ID=<the_flow_run_id>
PREFECT_API_KEY=<your_api_key>

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 '{
    "type": "CANCELLING",
    "message": "User requested cancellation"
  }'
2) Prefect Cloud – force cancel (bypass orchestration)
Copy code
curl -X POST \
  "<https://api.prefect.cloud/api/accounts/$ACCOUNT_ID/workspaces/$WORKSPACE_ID/flow_runs/$FLOW_RUN_ID/set_state?force=true>" \
  -H "Authorization: Bearer $PREFECT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "CANCELLED",
    "message": "Immediate cancellation requested"
  }'
3) Self-hosted Prefect Server – graceful cancel
Copy code
SERVER_URL=<http://127.0.0.1:4200>
FLOW_RUN_ID=<the_flow_run_id>

curl -X POST \
  "$SERVER_URL/api/flow_runs/$FLOW_RUN_ID/set_state" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "CANCELLING",
    "message": "User requested cancellation"
  }'
Notes - CANCELLING is a non-terminal state that prompts infrastructure-aware cleanup; the run will transition to CANCELLED when cleanup completes. - CANCELLED is terminal; using it with ?force=true will bypass orchestration checks. Use with care as it may not give infrastructure a chance to shut down cleanly. - Optional fields you can include: name, timestamp (ISO 8601), and state_details. Docs - Cancel workflows - Set flow run state (REST API) - States in Prefect If you share whether you’re on Cloud or self-hosted and whether you want graceful or forceful cancellation, I can tailor the exact command for your setup.