Always run (cleanup)

When a step fails, the run stops and the steps after it don't run. A step marked Always run still does, so test data is cleaned up whatever happened.

Example flow

examples/Help/always-run.clem.json creates a cart, does some work and deletes the cart in an Always run step. Set $simulateFailure to true in Variables and run it: the work fails, and the cleanup still runs.

Turning it on

Click the shield button on any step (or tick Always run, even after an earlier step failed in the request editor). The step then shows "always run".

A run where the second step failed, the third was not run, and the cleanup step marked always run passed
The work step failed; the cleanup step still ran.

How it behaves

flowRun

In code and run conditionals, flowRun says how the run went so far:

FieldValue
flowRun.failedtrue once a step failed
flowRun.status"passed" or "failed"
flowRun.failedSteps[{ index, name, message }]
// Runs even when a step before failed: flowRun says how the run went.
const r = await http({ method: "DELETE", url: $baseUrl + "/carts/1" });
log("deleted the test cart: " + r.status);
if (flowRun.failed) log("cleaned up after: " + flowRun.failedSteps.map((s) => s.name).join(", "));

(dummyjson.com doesn't keep the carts it creates, so the example deletes cart 1. Against your own API, delete "/carts/" + $cartId.)

Put cleanup steps at the end, and make each one safe to run when the data was never created: check the variable first (if ($cartId) { … }), or give the step the run conditional $cartId.