Skip to main content

Cancellation and termination

View Markdown

A business process that's already running sometimes has to stop: an order is retracted, a customer closes an account, a deploy goes wrong. Temporal gives you two ways to stop a Workflow Execution, and the difference between them is whether your code gets to clean up after itself.

Cancellation asks the Workflow to stop. The Workflow receives the request, so it can release resources, undo steps it has already taken, and notify other systems before closing. The steps it takes are your code, so you decide what unwinding a half-finished process means.

Termination closes the Workflow Execution immediately. Workflow code never sees the request and no cleanup runs. It works on a Workflow that can't respond to anything, which is what makes it the fallback when Cancellation won't land.

What this means for your design

  • You write the cleanup. Temporal delivers the Cancellation request and schedules a Workflow Task so your code can act on it. You can specify what to undo, and in what order. If your process reserves inventory, charges cards, or provisions infrastructure, plan the unwind path alongside the forward one. See the Saga Pattern for the usual structure.
  • Long-running Activities need Heartbeats to be cancellable. Cancellation reaches an Activity when it Heartbeats, so an Activity that never Heartbeats runs to completion regardless. This shapes how you write anything long-running.
  • You always have a way out. A Workflow blocked by a bug, a bad deploy, or a missing Worker can still be terminated, so a stuck process is never permanently stuck.
  • The record survives the stop. Both operations are recorded in Event History with who requested it and why, which matters for audit and incident review.

Cancellation and Termination close a Workflow for good. To hold one in place and resume it later, see Workflow Pause.

Resources

Read the Temporal Encyclopedia for conceptual depth:

Or jump straight to the SDK feature guide for implementation details: