pending or running.
Cancel a Workflow Run
Use thecancel() method on a workflow run handle:
Cancelable States
Only workflows in these states can be canceled:| Status | Can Cancel? |
|---|---|
pending | Yes |
running | Yes |
sleeping | Yes (legacy only) |
completed | No |
failed | No |
canceled | No (already canceled) |
Canceled Workflow Behavior
When a workflow is canceled:- The status changes to
canceled - The worker releases the workflow (sets
worker_idto null) - The
finished_attimestamp is set
Checking Cancellation Status
After callingcancel(), you can verify the status:
Idempotent Cancellation
Canceling an already-canceled workflow returns successfully without error:Use Cases
Common reasons to cancel workflows:- User-initiated cancellation - User decides to abort an operation
- Timeout exceeded - External system determines the workflow took too long
- Duplicate detection - Another workflow is already handling this work
- Cleanup on shutdown - Cancel pending work when shutting down a service