From 1d845e71eaebba7f35f9c3e7186ae68c3516016a Mon Sep 17 00:00:00 2001 From: vaish Date: Mon, 29 Jun 2026 11:23:51 +0530 Subject: [PATCH] Document workflow terminate rollback --- .../workflows/build/trigger-workflows.mdx | 15 ++++++++++ .../docs/workflows/build/workers-api.mdx | 28 +++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/content/docs/workflows/build/trigger-workflows.mdx b/src/content/docs/workflows/build/trigger-workflows.mdx index 4db3efcfe62..476081dd128 100644 --- a/src/content/docs/workflows/build/trigger-workflows.mdx +++ b/src/content/docs/workflows/build/trigger-workflows.mdx @@ -160,6 +160,21 @@ let instance = await env.MY_WORKFLOW.get("abc-123"); await instance.terminate(); // Returns Promise ``` +To run registered rollback handlers before terminating, pass `rollback: true`: + +```ts +let instance = await env.MY_WORKFLOW.get("abc-123"); +await instance.terminate({ rollback: true }); // Returns Promise +``` + +You can also run rollback handlers from Wrangler: + +```sh +npx wrangler workflows instances terminate --rollback +# For a local Workflows instance during wrangler dev: +npx wrangler workflows instances terminate --local --rollback +``` + Once stopped/terminated, the Workflow instance _cannot_ be resumed. ### Restart a Workflow diff --git a/src/content/docs/workflows/build/workers-api.mdx b/src/content/docs/workflows/build/workers-api.mdx index 973beac623c..c9f393f3ed4 100644 --- a/src/content/docs/workflows/build/workers-api.mdx +++ b/src/content/docs/workflows/build/workers-api.mdx @@ -477,7 +477,7 @@ declare abstract class WorkflowInstance { /** * Terminate the instance. If it is errored, terminated or complete, an error will be thrown. */ - public terminate(): Promise; + public terminate(options?: WorkflowInstanceTerminateOptions): Promise; /** * Restart the instance. */ @@ -523,7 +523,31 @@ Restart a Workflow instance. Terminate a Workflow instance. -- terminate(): Promise<void> +- terminate(options?: WorkflowInstanceTerminateOptions): Promise<void> + - `options` - optional properties that control how the instance is terminated. + +```ts +let instance = await env.MY_WORKFLOW.get("abc-123"); + +// Terminate without running rollback handlers. +await instance.terminate(); + +// Run registered rollback handlers before terminating. +await instance.terminate({ rollback: true }); +``` + +If `rollback` is `true`, Workflows runs the rollback handlers registered by completed or eligible steps before the instance reaches the `terminated` state. Steps without rollback handlers are skipped. + +#### WorkflowInstanceTerminateOptions + +```ts +interface WorkflowInstanceTerminateOptions { + /** + * If true, run registered rollback handlers before terminating the instance. + */ + rollback?: boolean; +} +``` ### sendEvent