Sub topic 7
Rolling back by redeploying the previous version
This chapter assumes you can read a package.xml manifest, run sf project deploy start, and use basic Git (commit, branch, revert). It also assumes you know how to tell a failed deployment from a successful deployment that caused problems. That distinction decides whether you need this chapter at all.
Why redeploying is the rollback
Salesforce has no "undo deployment" button. A deployment that succeeded is committed, and the platform keeps no record that lets it reverse the change. To go back, you deploy the older version of the components over the newer ones. The org then holds the metadata you had before the release.
A rollback is therefore a normal deployment with a different source. It follows the same rules as any other deployment:
- It is validated against the target org.
- It runs Apex tests when the target is production and the package contains Apex.
- It can fail on dependencies.
- It takes as long as a deployment of the same size.
Plan for that last point. If the release took 40 minutes to deploy, expect the rollback to take about as long. A rollback is not an instant action.
Choose the source of the previous version
You need a trustworthy copy of the old metadata. There are two common sources.
| Source | Strength | Weakness |
|---|---|---|
| Git history (tag or merge commit before the release) | Exact, reviewable, repeatable. Works with `git revert`. | Only as accurate as your repository. Changes made directly in the org are missing. |
| Backup retrieved from the org before the release | Reflects what the org actually contained, including manual changes. | You must have taken it. It is not reviewed or versioned unless you commit it. |
The first command in this chapter's examples, from the previous chapter on baselines, captures the second kind of source.
sf project retrieve start --manifest manifest/package.xml --target-org prod --output-dir backup/pre-release
sf project deploy validate --manifest manifest/package.xml --target-org prod --test-level RunLocalTestsThe retrieve saves the production versions of the components you are about to change. The validate checks the release without committing it. If you did both before the release, you have a backup ready. If your team treats Git as the source of truth, the pre-release tag or the commit before the merge serves the same purpose.
Rolling back from Git with `git revert`
When a release went to production through a merge commit, the cleanest rollback is to revert that merge.
git revert -m 1 <merge-commit-sha>
sf project deploy start --manifest manifest/package.xml --target-org prod --test-level RunLocalTestsThe -m 1 option matters. A merge commit has two parents, and Git must know which one is the mainline you want to return to. Parent 1 is the branch you merged into, so -m 1 restores the state of your main branch before the merge. Without -m, Git refuses to revert a merge commit.
git revert creates a new commit that undoes the change. It does not rewrite history. This is what you want for a shared branch: the history shows the release and the rollback, and nobody has to force-push.
A few points to check before you deploy the reverted source:
- Confirm that the working tree matches the pre-release state. Compare it against the tag you created before the release.
- Confirm that the manifest lists the components you want to restore.
- Confirm that you deploy to the right org. Rollbacks happen under stress, and
--target-orgis easy to get wrong.
git diff --name-status pre-release-2026-12-01 HEAD -- force-appAn empty diff means the source is identical to the pre-release tag. Any lines in the output are differences you should understand before deploying.
What the manifest controls
A deployment sends only what the manifest lists. That gives you control over the blast radius of the rollback, and it creates the main trap.
A rollback manifest for a release that changed one class and one field might look like this.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>OrderService</members>
<name>ApexClass</name>
</types>
<types>
<members>Order__c.Priority__c</members>
<name>CustomField</name>
</types>
<version>62.0</version>
</Package>Use the same API version your project uses. Prefer a manifest limited to the release's components over a manifest for the entire project. A full-project deploy overwrites every listed component in production, including ones changed by someone else since the release. A targeted deploy touches only the components you meant to restore.
Two limits follow from how deployment works.
- Redeploying overwrites, and does not merge. If an admin edited
OrderServiceor a page layout in production after the release, your rollback replaces that edit with the old version. Check who changed what before you deploy. - Redeploying does not delete. If the bad release added a new Apex class or custom field, reverting the source does not remove it from the org. The class stays in production. Removing added components is a separate step that uses a destructive changes manifest, covered in the next chapter. Also, once a revert removes a file from your project, a manifest that still lists that component can fail because the file no longer exists locally.
Deploy the rollback in production
Run the rollback as a validated deployment when you have time, and as a direct deployment when you do not.
sf project deploy start --manifest manifest/package.xml --target-org prod --test-level RunLocalTestsRunLocalTests runs every Apex test in the org except those from managed packages. This is the usual production choice and the safest one. The deployment reports its job ID when it starts. You can follow it with the sf project deploy report command from the report chapter.
You can reduce the rollback window with a validate-then-quick-deploy sequence:
- Run
sf project deploy validateon the rollback source. This runs the tests but commits nothing. - Review the result.
- Run
sf project deploy quick --job-id <validation-id>to commit the already-validated deployment without running the tests again.
A quick deploy must happen within 10 days of the validation, and it requires the validation to have run tests. If you prepared a rollback in advance, a validated rollback is the fastest way back.
Where rollbacks fail
Rollbacks fail in the same ways deployments do. These are the failures you will meet most often.
- Tests fail against the older code. The old version of a class may fail tests because data or other components changed. A rollback must pass the same test gate as the release.
- Missing dependencies. The old
OrderServicemay reference a field, class, or label that the release deleted or renamed. Restore those components in the same deployment. - A component is in use. You cannot overwrite or remove some components while something references them. A common example is a field used in a validation rule, a report, or an active flow.
- Partial restore. Restoring the Apex class but not the matching trigger, permission set, or layout leaves the org in a state you never tested.
- Profiles and permission sets. Profile files in source often contain only a subset of permissions. Redeploying one can leave newer permissions in place, so the org does not return to its exact earlier state.
If the rollback deployment fails, it is atomic like any other deployment. Nothing from the rollback is saved, and the org stays in the broken state. Read the report, fix the cause, and run it again.
The decision path
The following diagram shows how to go from a bad release to a deployed rollback.
Verify after the rollback
A successful rollback deployment only means the metadata was accepted. Confirm the outcome:
- Retrieve the components again and diff them against the backup or tag. They should match.
- Run the user-facing check that exposed the original problem.
- Check that automations behave as before, such as flows and triggers.
- Record the job ID, the commit SHA you deployed, and the time. You will need this in the runbook.
This verification step catches the case where a profile, a flow version, or a manual org change kept part of the release alive after the deploy.
Common mistakes
- Reverting the source and forgetting to deploy it. The revert commit changes your repository, not the org.
- Reverting with
git reset --hardand force-pushing. This destroys shared history. Usegit revert. - Deploying a whole-project manifest and overwriting unrelated later changes.
- Assuming the deployment removed new components. It did not.
- Skipping validation because of time pressure, then failing a 40-minute deploy on a test.
Exercise
A release merged into main as commit 9f3c2a1 added the class OrderHelper and changed the class OrderService. It deployed successfully but breaks order processing. Write the two commands that restore the old OrderService from Git and deploy it to the org with alias prod. Then say what is still wrong in production afterward and why.
Show answer
Run git revert -m 1 9f3c2a1 to undo the merge on main (-m 1 keeps the mainline parent), then sf project deploy start --manifest manifest/package.xml --target-org prod --test-level RunLocalTests. The manifest should list OrderService. After the deploy, OrderHelper is still in production, because redeploying source never deletes components that the bad release added. You need a destructive changes manifest that lists OrderHelper to remove it.