Skip to main content

Run.cancel()

The .cancel() method cancels a workflow run, stopping execution and cleaning up resources.

This method aborts any running steps and updates the workflow status to 'canceled'. It works for both actively running workflows and suspended/waiting workflows.

Usage example
Direct link to Usage example

const run = await workflow.createRun();

await run.cancel();
// Returns: { message: 'Workflow run canceled' }

Parameters
Direct link to Parameters

No parameters:

void
This method takes no parameters

Returns
Direct link to Returns

result:

Promise<{ message: string }>
A promise that resolves with { message: 'Workflow run canceled' } when cancellation succeeds

How cancellation works
Direct link to How cancellation works

When called, the workflow will:

  1. Trigger the abort signal - Uses the standard Web API AbortSignal to notify running steps
  2. Prevent subsequent steps - No further steps will be executed

Abort signal behavior
Direct link to Abort signal behavior

Steps that check the abortSignal parameter can respond to cancellation:

  • Steps can listen to the 'abort' event: abortSignal.addEventListener('abort', callback)
  • Steps can check if already aborted: if (abortSignal.aborted) { ... }
  • Useful for cancelling timeouts, network requests, or long-running operations

Note: Steps must actively check the abort signal to be canceled mid-execution. Steps that don't check the signal will run to completion, but subsequent steps won't execute.

Extended usage examples
Direct link to Extended usage examples

Cancelling a workflow on error
Direct link to Cancelling a workflow on error

const run = await workflow.createRun();

try {
const result = await run.start({ inputData: { value: "initial data" } });
} catch (error) {
await run.cancel();
}

Creating a step that responds to cancellation
Direct link to Creating a step that responds to cancellation

const step = createStep({
id: 'long-running-step',
execute: async ({ inputData, abortSignal, abort }) => {
const timeout = new Promise((resolve) => {
const timer = setTimeout(() => resolve('done'), 10000);

// Clean up if canceled
abortSignal.addEventListener('abort', () => {
clearTimeout(timer);
resolve('canceled');
});
});

const result = await timeout;

// Check if aborted after async operation
if (abortSignal.aborted) {
return abort(); // Stop execution
}

return { result };
}
});