Sub topic 1
What Salesforce Does When a Deployment Fails
Salesforce has no rollback button for deployments. Even so, a failed deployment usually fixes itself, because the platform undoes the work before it finishes. This chapter explains what that automatic undo covers, what it leaves alone, and how to confirm the state of your org after a failure. Later chapters cover the harder case: a deployment that succeeds and then needs to be reversed.
Prerequisites
This chapter assumes you have:
- Basic Salesforce metadata knowledge: Apex classes, custom fields, flows, and profiles.
- Some familiarity with
package.xmlmanifests. - Experience deploying with Salesforce CLI, change sets, or an IDE.
- Basic Git usage: commit, revert, and branches.
The examples use the sf CLI (v2) and an org alias named prod. Replace it with your own alias.
The all-or-nothing model
A Metadata API deployment is a single unit of work. It is not a series of independent component saves. The platform processes it in roughly this order:
- It queues the deployment and validates the request.
- It saves each component to the org, compiling Apex and checking dependencies as it goes.
- It runs the Apex tests required by the test level.
- It commits the whole change only if every component saved and every required test passed.
If any component fails to save, or any required test fails, the platform rolls back the entire deployment. No component from the package stays in the org, including components that saved without errors. Salesforce also reverts any data the tests created, because Apex test data is never committed.
This is why a failed deployment is rarely a rollback problem. If the report says Failed, the org is normally in the same metadata state as before you started. Your job is to fix the cause and deploy again.
Where the tests fit
The tests run after the components are saved, but before the commit. A deployment can therefore save every component cleanly and still end as Failed because one test failed or code coverage was too low. The platform treats that as a failure and rolls back the saved components.
The test level you choose decides which tests can cause that failure.
| Test level | What runs | Typical use |
|---|---|---|
| `NoTestRun` | No tests | Sandbox and scratch orgs only. Not allowed for production deployments that include Apex. |
| `RunSpecifiedTests` | Only the tests you name | Targeted production deployments. Each deployed class and trigger needs enough coverage from the named tests. |
| `RunLocalTests` | All tests in your org except managed package tests | Common choice for production. |
| `RunAllTestsInOrg` | Every test, including managed package tests | Rare. Slow. |
If you do not set a level, Salesforce picks a default. For production it is RunLocalTests when the deployment contains Apex classes or triggers, and NoTestRun otherwise. Set the level explicitly so the behavior is the same on every run.
The exception: ignoring errors
The Metadata API has a rollbackOnError option. When it is true, any error rolls back the whole deployment. Production orgs always behave this way. In a sandbox or scratch org you can turn it off. The CLI exposes this as --ignore-errors:
sf project deploy start --manifest manifest/package.xml --target-org my-sandbox --ignore-errorsWith this flag, components that deploy successfully stay in the org even if others fail. The deployment ends as SucceededPartial. That is useful when you are iterating in a development sandbox and want to see which of 300 components are broken. It is dangerous as a habit, because the org ends up in a mixed state that matches neither your source nor the previous version.
Reading the outcome from the CLI
After a deployment ends, check its status before you assume anything. The CLI remembers the most recent deployment, so you can ask for its report without the job ID:
sf project deploy report --use-most-recent --target-org prodFor scripts, add --json and read the status field:
sf project deploy report --use-most-recent --target-org prod --jsonThe values that matter in this chapter are:
Succeeded: everything was committed.Failed: nothing was committed. The org metadata is unchanged.SucceededPartial: some components were committed and some were not. This is possible only when errors are ignored outside production.Canceled: you or someone else stopped the deployment. Nothing was committed.
The details of reading component failures and test failures come in the next chapter. For now, the key point is that the status is the first thing to check, because it tells you whether any rollback work exists at all.
What the automatic rollback does not cover
The rollback applies to the deployment transaction. Several things sit outside it.
Changes made by other people
The rollback restores the components in your package. It does not touch anything else. If an admin edited a field in Setup while your deployment ran, that edit is not part of your failed deployment, and it is not undone.
Actions taken before the deployment
Your own preparation steps are not rolled back. Examples include a data load that ran before the deploy, a permission set assignment, or a manual change made to prepare the org. If your release plan has steps before and after the deployment, a failure at the deployment step leaves those other steps in place.
Actions taken after a partial pipeline run
A release often chains steps: deploy metadata, run a data script, activate a flow. When the deploy step fails, a pipeline may stop correctly. A badly written script might continue and run the later steps against an org that did not receive the new metadata. Make later steps depend on the exit code of the deploy step.
Side effects that tests produce outside the database
Test data is rolled back, but anything a test sends outside the org is not. Tests cannot make real callouts, and they do not send real emails, so this is rarely a problem. Custom code that writes to an external system during deployment, for example through a post-install script in a package, is not covered by the platform's rollback.
Why a "Failed" result can still hurt
A failed deployment leaves metadata unchanged, but it still has costs:
- Time lost. A
RunLocalTestsrun on a large org can take an hour or more. If a test fails near the end, you have lost that hour and your release window. - Locks and queueing. Other deployments to the same org wait in the queue. A long-running deployment can delay unrelated work.
- False confidence. If you read "deployment failed" as "nothing changed" without checking, you may miss that someone ran a second deployment, or that the failure was in a different job than you think.
These costs are the reason to validate first, which a later chapter covers. A validation runs the same checks and the same rollback logic, but it never commits, so you find out about failures before the release window.
A quick check after any failure
Use this sequence when a deployment ends in Failed:
- Confirm the status and the job ID with
sf project deploy report. - Read the component and test failures and fix the cause in source.
- Check whether anyone else deployed or edited the same components in the meantime.
- Redeploy, or validate again first if the release window is tight.
You do not need to restore anything from a backup in this case. That distinction is the main takeaway of the chapter. Backups and redeploying a previous version become necessary when a deployment ends in Succeeded and the result is wrong.
Summary of behaviors
| Situation | What Salesforce does | What you do |
|---|---|---|
| A component fails to save | Rolls back the whole deployment | Fix the component, redeploy |
| A required test fails | Rolls back the whole deployment | Fix the code or test, redeploy |
| Coverage is too low | Fails and rolls back | Add tests, redeploy |
| Errors ignored in a sandbox | Keeps successful components, ends `SucceededPartial` | Fix the failures, redeploy the rest |
| Deployment is canceled | Rolls back, ends `Canceled` | Decide whether to retry |
| Deployment succeeds but is wrong | Nothing, the change is committed | Recover manually (later chapters) |
Exercise
A production deployment with --test-level RunLocalTests saved all 40 components without errors, but one Apex test failed. The report status is Failed. Is any of the 40 components now in the org, and what should you do next?
Show answer
No. The platform treats a test failure as a deployment failure and rolls back the whole deployment, including components that saved cleanly. Production always rolls back on error. The org metadata is unchanged, so no backup or restore is needed. Check the status with sf project deploy report, read the test failure, fix the code or test, and redeploy.
Exercise
Why is --ignore-errors a poor choice for rehearsing a production release in a sandbox?
Show answer
It lets components that deploy successfully stay in the org while others fail, and the deployment ends as SucceededPartial. Production never behaves this way, because it always rolls back on error. The rehearsal would hide dependency problems and leave the sandbox in a mixed state.