Sub topic 6
Canceling a Deployment That Is Still Running
This chapter assumes you can run sf project deploy start and read a package.xml. It also assumes you know that a deployment runs component checks and Apex tests on the server. The previous chapters covered what happens when a deployment fails on its own. Here you will learn how to stop one yourself, when that is worth doing, and what state the org is left in.
Why you would cancel
A deployment that is still running has not changed your org yet. That makes canceling the cheapest form of rollback, because there is nothing to undo. People cancel for these reasons:
- You notice a wrong manifest, branch, or target org right after starting.
- A test run with
RunLocalTestsis taking far longer than expected, and you want to free the deployment queue. - A stakeholder asks you to stop the release during the maintenance window.
- A pending deployment is blocking a more urgent fix that is waiting behind it.
Cancel only deployments that have not finished. If the report already shows Succeeded, there is nothing left to cancel and you need the redeploy path from the next chapters.
What the org looks like while a deployment runs
The Metadata API processes a deployment as a job with a job ID. In production, and in any deployment with rollbackOnError behavior, the job is all-or-nothing. Components are checked and tests run, and the changes are committed only if everything passes. Until that commit, the org still holds the old metadata.
The job moves through these statuses:
| Status | Meaning | Can you cancel it? |
|---|---|---|
| Pending (Queued) | Waiting for earlier work to finish | Yes |
| In Progress | Deploying components or running tests | Yes, with a caveat below |
| Canceling | A cancel request was accepted and is being processed | Already requested |
| Canceled | The job stopped and nothing was committed | Finished |
| Succeeded | All changes were committed | No |
| Failed | Errors stopped the job and nothing was committed | No |
The Canceling state exists because cancellation is a request, not an instant stop. The server needs time to interrupt running tests and abandon the work. The Canceling to Succeeded edge is a real risk. If your request reaches the server when the job is already in its final commit, the job can finish normally. Always confirm the final status instead of assuming the cancel worked.
Getting the job ID
Every cancel and report command needs the job ID, or a shortcut to the most recent job. The ID starts with 0Af. You can get it in three ways:
- Start the deployment with
--async. The command returns immediately and prints the ID. - Read it from the output of a deployment that is already running in another terminal.
- Open Setup, search for Deployment Status, and read the ID from the In Progress or Pending table.
sf project deploy start --manifest manifest/package.xml --target-org prod --test-level RunLocalTests --asyncThe output includes a line similar to Deploy ID: 0AfXXXXXXXXXXXXXXX. Save it in your release notes. Under pressure, hunting for the ID wastes time.
Canceling with the Salesforce CLI
First check what the job is doing, then cancel it:
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prod
sf project deploy cancel --job-id 0AfXXXXXXXXXXXXXXX --target-org prodThe report command shows the status, the number of components processed, and the number of tests run. The cancel command sends the cancel request. By default it waits for the cancellation to complete, so you see the final status in your terminal.
If you started the deployment from this machine a moment ago, you can skip the ID:
sf project deploy cancel --use-most-recent --target-org prodThe CLI keeps a record of recent deployment jobs for this purpose. Use --use-most-recent only when you are certain which job is the most recent. If a teammate or a CI job deployed to the same org from another machine, your local record does not know about it. In that case use the explicit --job-id.
If you do not want the terminal to wait, send the request and check later:
sf project deploy cancel --job-id 0AfXXXXXXXXXXXXXXX --target-org prod --async
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prodRun the report until it shows Canceled, or until it shows another final status that tells you the cancel came too late.
Canceling from Setup
If you do not have the CLI at hand, use the browser:
- Open Setup and search for Deployment Status.
- Find the deployment in the In Progress or Pending table.
- Click Cancel next to it.
- Reload the page until the job appears in the Failed or Canceled list, or in the Succeeded list if the cancel was too late.
This page also covers deployments started from change sets, an IDE, or another tool, because they all use the same job queue. That makes it the best option when you do not know who started the job.
What canceling does and does not do
Canceling a running validation or deployment stops the work and leaves the org's metadata as it was before the job started. Three details matter for intermediate users.
Nothing is committed. Because the job is all-or-nothing, a canceled job does not leave half-deployed components. You do not need a backup restore or a destructive changes file after a successful cancel. The baseline you captured earlier is still worth having, but you do not need to use it.
Side effects of running tests can remain. Apex tests normally roll back their own data, but a test that makes a callout or has other effects outside the database can still leave traces. Tests that use SeeAllData=true and write to records, or tests that publish platform events, deserve a check after a cancel. If your tests are well isolated, there is nothing to clean up.
The queue moves on. When the job reaches Canceled, the next pending deployment starts. If you canceled to unblock a hotfix, confirm that the hotfix job starts and that no third deployment is ahead of it.
When a cancel arrives too late
The final commit phase is short compared with the test phase, but it is not zero. If the report shows Succeeded after your cancel request, treat the situation as a successful but unwanted release. Switch to the recovery path from the earlier chapter on choosing between a failed deployment and a bad release: redeploy the previous version from source control, and use destructive changes for components the release added.
You can reduce the chance of this by deciding early. A cancel during the test phase, which is the longest part of a production deployment, is far more likely to work than one made after the report shows all tests passed.
Canceling a validation and a quick deploy
A validation (sf project deploy validate) is also a job, with its own job ID. You can cancel it the same way. Canceling a validation has no effect on production metadata, because validations never commit changes.
A quick deploy is a different case. It reuses the result of a successful validation and skips running tests again, so it finishes much faster. Because the window is short, do not plan on canceling a quick deploy. Make the decision before you start it. The earlier chapter on validation and quick deploy explains why that decision belongs in the validation step.
A script for your release terminal
If you start deployments from a script, store the ID so a cancel is one command away:
#!/usr/bin/env bash
set -euo pipefail
ORG="prod"
MANIFEST="manifest/package.xml"
job_id=$(sf project deploy start \
--manifest "$MANIFEST" \
--target-org "$ORG" \
--test-level RunLocalTests \
--async \
--json | jq -r '.result.id')
echo "$job_id" > .last-deploy-id
echo "Started deployment $job_id"
echo "To cancel: sf project deploy cancel --job-id $job_id --target-org $ORG"
sf project deploy resume --job-id "$job_id" --target-org "$ORG"The script needs jq installed. It writes the ID to a file, prints the cancel command, and then resumes watching the job so the output streams to your terminal. If you press Ctrl+C at that point, the job keeps running, and the file and the printed command give you what you need to stop it.
Common mistakes
- Canceling the wrong job. With several deployments in the queue, check the ID and the target org in
sf project deploy reportbefore you cancel. - Assuming Ctrl+C cancels. It does not. See the warning above.
- Not verifying the result. A cancel request that is accepted can still end in
Succeeded. Always read the final status. - Canceling and immediately redeploying. Wait for
Canceled. While the status isCanceling, your new job may queue behind it or collide with it. - Using
--use-most-recentin shared automation. Pass an explicit job ID in CI. - Treating cancel as a fix. Canceling stops a deployment. It does not fix the manifest, the tests, or the plan that caused the problem.
Exercise
You start a production deployment with sf project deploy start --test-level RunLocalTests and notice 5 minutes later that it uses the wrong branch. You press Ctrl+C in the terminal. A teammate then says the deployment still shows In Progress in Setup. What do you do, and how do you decide whether you also need a rollback?
Show answer
Ctrl+C only stopped the CLI from waiting, so the job is still running. Get the job ID from Setup > Deployment Status, or from the CLI output if you saved it. Run sf project deploy cancel --job-id <id> --target-org prod, then run sf project deploy report --job-id <id> --target-org prod until the status is final. If the status is Canceled, nothing was committed and no rollback is needed. If the status is Succeeded, the cancel came too late and you must roll back by redeploying the previous version from source control, with destructive changes for any components the release added. If the status is Failed, nothing was committed either, and you only need to fix the cause.