Sub topic 9
Rolling Back When You Used Change Sets
Change sets are the point-and-click way to move metadata between orgs. They have no rollback feature. This chapter explains why, what you can do about it, and how to build a rollback that uses change sets, the CLI, or both.
This chapter assumes you know how to build outbound change sets and deploy inbound ones. It also assumes you can read a package.xml manifest and use Git or an equivalent backup. Earlier chapters covered failed deployments, backups, redeploying a previous version, and destructive changes. Here you apply those ideas to the limits of change sets.
What change sets cannot do
Three properties of change sets shape every rollback:
- They only add or overwrite. A change set carries components from a source org. Deploying it creates the components or replaces them with the incoming version. A change set cannot delete a component from the target org.
- They are not versioned. Once an inbound change set deploys, the target org holds the new version. Salesforce does not keep the old version for you to restore. The Deployment Status page shows the result of the deployment, but it has no undo.
- They are tied to a source org. An outbound change set is built from whatever is in the source org when you upload it. If you want to put an old version back, some org must contain that old version.
The all-or-nothing behavior from the first chapter still applies. A change set deployment that fails leaves the target org unchanged, so you do not need to roll back a failed change set. You need a rollback only when a deployment succeeds and the release turns out to be harmful.
Choosing a rollback path
The right path depends on whether you have a backup, and on whether the release added components or only changed existing ones.
The two questions matter because of the asymmetry in change sets. Restoring changed components is something change sets do well, because overwriting is what they do. Removing added components is something they cannot do.
Record what the release contained
Before you can reverse a release, you need to know exactly what it changed. A change set gives you that list in two places:
- The outbound change set in the source org, under Change Set Detail, lists its components.
- The inbound change set in the target org lists the same components after upload.
Copy this list into your release notes or a package.xml at the time of the release. If you wait until something breaks, the outbound change set may have been edited or deleted, and you will be rebuilding the list from memory under pressure.
Components you did not list cannot be restored from a backup, because you never knew to back them up. This is the most common failure in change set rollbacks. Dependencies that the change set tool added automatically (for example through View/Add Dependencies) are part of the release too, so record them as well.
Capture a baseline before you deploy
Because Salesforce keeps no previous version, you must create one. The following command retrieves the current production versions of the components in your release manifest:
sf project retrieve start --manifest manifest/package.xml --target-org prod --output-dir backup/pre-releaseThis works even though you deploy through change sets. Both the CLI and change sets use the Metadata API, so you can take a backup with one tool and release with the other. Commit the retrieved files to Git on a branch or tag named for the release, so the baseline can be found later.
Some component types cannot be added to a change set but can be retrieved, and some behave the other way around. If your manifest lists a type that change sets do not support, you will not be able to restore it with a rollback change set, though the CLI can usually deploy it.
Rollback option 1: a rollback change set
If you want to stay inside the change set workflow, you need an org that holds the old versions. This usually means a sandbox.
- Create or pick a sandbox that is connected to production by a deployment connection.
- Deploy the backup versions into that sandbox. Use the files in
backup/pre-release:
``bash sf project deploy start --source-dir backup/pre-release --target-org rollback-sandbox ``
- In the sandbox, create an outbound change set that contains exactly the components from the release list.
- Upload it to production and wait for it to appear as an inbound change set.
- Validate it, then deploy it. Use the same test level you used for the original release.
There are several things to check before step 3:
- Other differences in the sandbox. An outbound change set picks up the sandbox's version of each component you add. If someone else changed one of those components in the sandbox, the change set carries their change to production. Compare the sandbox against the backup before you build the change set.
- Time. Deploying to a sandbox, building the change set, and uploading it takes much longer than a single CLI command. During an incident this delay matters.
- Profiles and permission sets. These are overwritten with the sandbox's version of the included sections. If the sandbox profiles differ from production, you may remove access from users. Check these two types more carefully than the others.
Rollback option 2: use the CLI for the rollback
You can roll back a change set release with the CLI, and this is usually the faster path. It needs only the backup you captured earlier:
sf project deploy start --manifest manifest/package.xml --source-dir backup/pre-release --target-org prod --test-level RunLocalTestsIf your backup is in Git and the release was merged, git revert -m 1 <merge-commit-sha> followed by a deploy does the same job from source control. The manifest must list the same components as the release, or the deployment will not touch them.
Mixing tools is acceptable. The org does not record whether a component arrived through a change set or the CLI. The caveat is process: if your team's change control requires a change set for every production change, a CLI rollback may need approval as an exception. Decide this in advance, not during the incident.
Deleting components the release added
Suppose the release added an Apex class named OrderHelper. Redeploying the old versions will not remove it, because the old versions never included it. Change sets cannot delete it either. You have two choices.
Delete it by hand in Setup. This works for a small number of components. Deletion may be blocked if other components depend on the one you are deleting, so you may need to delete in dependency order.
Use a destructive changes manifest with the CLI. This is more reliable when there are many components, and it is repeatable:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>OrderHelper</members>
<name>ApexClass</name>
</types>
</Package>Save this as destructiveChangesPost.xml and deploy it together with the restored components:
sf project deploy start --manifest manifest/package.xml --post-destructive-changes destructiveChangesPost.xml --target-org prod --test-level RunLocalTestsThe post-destructive file is applied after the other components are deployed. This order lets a restored component stop referring to OrderHelper before the class is removed. Use a pre-destructive file when the old component must disappear first, for example when it would conflict with a restored one.
Edge cases and gotchas
| Situation | What happens | What to do |
|---|---|---|
| The release changed a component that someone edited directly in production afterward | The rollback overwrites the later edit | Retrieve the current version first and diff it against the release version |
| The release added a field that now holds data | Deleting the field deletes the data | Export the data before deleting, or leave the field and hide it |
| The rollback depends on a component the release deleted by hand | The deployment fails on a missing reference | Include the missing component in the backup, or restore it first |
| The original change set was validated, and you want to reuse its quick deploy | Quick deploy applies only the validated release, not the old version | Do not use quick deploy for a rollback unless you validated the rollback itself |
| The change set had a profile with record type or layout assignments | Overwriting the profile may reset assignments | Review the profile diff before deploying |
Quick deploy deserves a specific mention. A validated change set can be deployed without rerunning tests, but only for a limited window after validation. A rollback is a separate deployment, so validate it separately if you have time. That also tells you whether the rollback would fail before production is touched.
Cancel instead of rolling back
If a change set deployment is still in progress and you realize it is wrong, cancel it from the Deployment Status page in Setup. A canceled deployment leaves the org as it was before, so no rollback is needed. Do this as soon as you notice, because once the deployment finishes the cancel option is gone.
A minimal change set rollback checklist
Use this as the seed of your runbook:
- Before release, record the component list and retrieve a backup into Git.
- Decide who can approve a rollback and whether the CLI is allowed for it.
- After a bad release, retrieve the current production state and compare it with the release version.
- Identify components that were added and those that were only changed.
- Redeploy the backup versions, by rollback change set or CLI.
- Delete the added components with a destructive changes file or by hand.
- Check the data changes the release caused, since no metadata rollback restores those.
Exercise
A change set release added a custom field and changed an existing Apex class. It succeeded but breaks order processing. You have a backup of the Apex class from before the release. Which steps return production to its earlier state, and what must you check before removing the field?
Show answer
Redeploy the backed-up Apex class, using a rollback change set built from a sandbox that holds the old version or a CLI deploy from the backup. Then remove the added field, because the old versions do not include it and a change set cannot delete it. Delete it in Setup or with a destructive changes manifest. Before deleting the field, check whether it already holds data, because deleting a custom field removes that data. Export or back up the values first. Also check for components that reference the field, because they can block the deletion.
Exercise
Your team releases only through change sets and takes no backups. A release has just broken a flow and a validation rule, and both existed before the release. What is the fastest way to find the old versions, and what should the team change afterward?
Show answer
Look for an org that still holds the old versions, such as a sandbox not refreshed since the release or a Git history that tracked production. Deploy those versions to production by change set or CLI. If no such copy exists, the team must rebuild the old versions by hand from documentation. Afterward, the team should retrieve a baseline into source control before every release and record the component list from each change set.