Sub topic 7
Rolling Back by Redeploying the Previous Version
Salesforce has no rollback button for a successful deployment. To undo a release, you deploy the previous versions of the changed components. This chapter shows how to do that from source control.
This chapter assumes you know package.xml manifests, basic Git (commit, revert, branches), and how to deploy with Salesforce CLI. It also assumes you have a baseline of the previous state, either a Git history or a retrieved backup.
The working model
A deployment overwrites components in the org with the versions you send. A rollback is a second deployment that sends the older versions. The org does not remember what it held before, so your source of truth for "before" must live outside the org.
Step 1: Revert the release in Git
If your release went through a merge commit, revert that commit. A merge commit has two parents, so you must tell Git which parent to keep. -m 1 keeps the first parent, which is normally the branch you merged into.
git revert -m 1 <merge-commit-sha>
sf project deploy start --manifest manifest/package.xml --target-org prod --test-level RunLocalTestsgit revert creates a new commit that undoes the old one. It does not rewrite history, so the rollback is recorded and can itself be reverted later.
Step 2: Deploy the reverted source
The deploy command in the block above sends every component named in manifest/package.xml. Those components now have their previous content, so they overwrite the bad versions in the org.
Use --test-level RunLocalTests for a production rollback. Production runs tests on any deployment that includes Apex, and a rollback is no exception. A rollback can also fail on test failures, which is why you should check the previous version against the current org state before an emergency, not during one.
If you have no Git history
If the previous version was never committed, use the backup you retrieved before the release:
sf project deploy start --source-dir backup/pre-release --target-org prod --test-level RunLocalTestsHere --source-dir points at the retrieved files. The backup must be in source format. If you retrieved with a manifest and --output-dir, check that the output folder holds the components you expect before you deploy it.
Step 3: Handle components the release added
Redeploying the old versions restores components that existed before. It does not remove components that the release created, because the old source does not mention them. Those components stay in the org. The next chapter covers removing them with a destructive changes manifest.
Here is how the cases differ.
| Release change | Redeploy of old source restores it? |
|---|---|
| Modified Apex class | Yes, the old body overwrites the new one |
| Modified custom field (label, help text) | Yes |
| New Apex class | No, it stays until you delete it |
| New custom field | No, it stays until you delete it |
| Deleted component | Only if the old source still contains it |
Common errors
- Reverting the wrong parent. Using
-m 2reverts to the feature branch state, which can reapply the bad code. Check the result withgit diff HEAD~1before you deploy. - A manifest that omits components. If
package.xmllists only some of the changed components, the others keep their new versions. Build the manifest from the release's full list. - Dependencies that block the deploy. If another component now references a new field or class, the old source may not deploy cleanly. Read the failure report and fix the order of operations.
- Test failures in production. The old code may fail tests that were updated in the release. Revert the tests along with the code, in the same commit.
- Deploying from the wrong branch. Confirm your working tree is at the reverted commit before you run the deploy.
Verify the rollback
After the deploy succeeds, check the deployment status and spot-check a component that changed:
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prodThen open a changed class or field in Setup and compare it with the previous version in Git.
Exercise
A release modified the Apex class InvoiceService and added a new class InvoiceHelper. You revert the merge commit and deploy the reverted source. What state is the org in, and what do you still need to do?
Show answer
InvoiceService returns to its previous version, because the reverted source overwrites it. InvoiceHelper is still in the org, because the old source does not mention it and a normal deployment never deletes components. You still need to remove it with a destructive changes manifest.
Exercise
Why does git revert -m 1 <sha> need the -m 1 option for a merge commit?
Show answer
A merge commit has two parents, and Git cannot tell which side of the merge to keep. -m 1 selects the first parent, normally the target branch, so the revert undoes the changes the merged branch brought in.