Sub topic 1
What Salesforce does when a deployment fails
Prerequisites
This chapter assumes you can:
- Read a
package.xmlmanifest and know what Apex classes, custom fields, and flows are. - Run
sf project deploy startfrom the Salesforce CLI, or deploy with an IDE or change set. - Read basic Git output (commits, branches).
Why this matters
Many teams ask for a "rollback button" after a failed release. Salesforce has no single button for deployments. For one situation, a deployment that fails, you often don't need one, because the platform undoes the deployment for you. This chapter explains that behavior. Later chapters cover the harder case, where a deployment succeeds and you still need to undo it.
All-or-nothing deployments
A Metadata API deployment is transactional. Salesforce treats everything in the deployment as one unit:
- If every component saves and every required test passes, Salesforce commits all the changes.
- If any component fails to save, or any required test fails, Salesforce commits none of them.
A failed deployment leaves your org as it was before you started. Components that were valid on their own are discarded too, because they belong to the same unit. You don't need to delete or restore anything after a failed deployment.
What counts as a failure
These problems each cause the whole deployment to roll back:
- A component error, such as an Apex compile error, or a flow that references a field that doesn't exist in the target org.
- A missing dependency, such as a custom field referenced in a layout that is neither in the deployment nor in the org.
- A failing Apex test in a run that includes tests.
- Code coverage below the required threshold, when coverage is enforced. Production requires at least 75% overall Apex coverage, and each class or trigger in the deployment must have at least 1% coverage.
Production deployments that include Apex run tests by default, so a test failure is the most common reason a release that compiled fine still fails.
The exception: partial deployment in sandboxes
The all-or-nothing rule can be switched off, but only outside production. In the Metadata API this is the rollbackOnError option. In the Salesforce CLI it is the --ignore-errors flag.
sf project deploy start --manifest manifest/package.xml --target-org my-sandbox --ignore-errorsWith this flag, Salesforce commits the components that succeeded and reports the ones that failed. The deployment finishes with a status of SucceededPartial. Production always requires rollback on error, so you can't use this flag there. If you want partial results in a sandbox, check the report afterward and don't assume the org matches your source.
Deployment status values
A deployment is asynchronous. You submit it and Salesforce works through it in the background. The status moves through these values:
| Status | Meaning |
|---|---|
| `Pending` | Queued, not started |
| `InProgress` | Saving components or running tests |
| `Succeeded` | Everything committed |
| `SucceededPartial` | Some components committed (sandbox, rollback disabled) |
| `Failed` | Rolled back, org unchanged |
| `Canceling` / `Canceled` | A cancel was requested, then finished |
Failed is the safe outcome. Succeeded only tells you the platform accepted the changes. It doesn't tell you the changes are correct, and that distinction is the focus of the chapter on choosing a recovery path.
What a rollback does not cover
The rollback applies to the metadata in the deployment transaction. Be careful about two cases:
- Data. Metadata deployment doesn't normally create or change business records, so rollback or no rollback, your data is untouched. Apex tests run in a transaction that is discarded, but a test that is marked to see org data (
SeeAllData=true) or code that makes callouts can still have effects outside Salesforce. - Time. While a deployment is
InProgress, some metadata changes in the target org can be blocked. A failed deployment still takes its full run time to fail, so a large test run that fails at the end can occupy your release window.
Exercise
A production deployment of 40 components fails because one Apex test fails. Thirty-nine components compiled correctly. In what state is the org afterward, and what do you need to restore?
Show answer
The org is unchanged. Because the deployment is all-or-nothing, none of the 40 components were committed, including the 39 valid ones. You don't need to restore anything. You fix the failing test and deploy again.
Exercise
Why can't you use the --ignore-errors flag to deploy only the working components to production?
Show answer
Production always requires rollback on error, so partial deployment is only available in sandboxes and scratch orgs. In production, a single error rolls back the whole deployment.