Sub topic 9
Rolling Back When You Used Change Sets
This chapter assumes you know the Salesforce CLI commands from the earlier chapters on backups and redeploying a previous version. It also assumes you have deployed with outbound and inbound change sets before.
Why change sets are harder to roll back
A change set is a bundle of components that you upload from one org and deploy in another. Three properties of change sets shape any rollback:
- An inbound change set has no undo. Once it deploys, the target org holds the new versions, and no button restores the old ones.
- A change set cannot delete anything. It adds or overwrites components only.
- A change set always carries the versions that exist in the source org at upload time. Cloning it later does not bring back the old versions.
The all-or-nothing rule still applies. If the deployment fails, Salesforce rolls back the whole deployment and the target org is unchanged. This chapter covers the other case: the deployment succeeded and the release is harmful.
Choosing a rollback path
Which path you take depends on whether you have a trustworthy copy of the old versions.
Option 1: redeploy a backup with the CLI
You can roll back a change set deployment with the Salesforce CLI. The deployment method used for the release does not matter, because the backup is plain metadata. This is the cleanest path, and it only works if you retrieved the old versions before the release.
List the components the change set contained in a manifest. Then retrieve them from production before deploying the change set:
sf project retrieve start --manifest manifest/package.xml --target-org prod --output-dir backup/pre-releaseIf the release goes wrong, deploy that backup back to production:
sf project deploy start --metadata-dir backup/pre-release --target-org prod --test-level RunLocalTestsThe --metadata-dir flag deploys metadata-format files. This matches the folder that --output-dir produced. If you keep a Git baseline instead, check out the baseline commit and use --manifest as shown in the redeploy chapter.
Option 2: build a reverse change set
You can do this without the CLI, but only if a sandbox still holds the old versions. The sandbox must not have been refreshed or overwritten with the release changes. The steps are:
- Confirm the sandbox's versions match what production had before the release. Compare against your records or a backup.
- Create a new outbound change set in that sandbox with the same components.
- Upload it to production and deploy it as an inbound change set. Run the tests your org requires.
Most teams cannot satisfy step 1. The development sandbox where you built the release normally holds the new versions, so a clone of the original change set only reapplies the harmful release.
Removing components the release added
Rolling back versions does not remove components that the release created. A new Apex class stays in production after you restore the old versions of everything else. A change set cannot delete it. You have two options:
- Delete it by hand in Setup. This works for a few components. Salesforce blocks a delete if another component still references the target, so remove the references first.
- Deploy a destructive changes manifest with the CLI. This is better for many components or when you need an audit trail.
sf project deploy start --manifest manifest/package.xml --post-destructive-changes destructiveChangesPost.xml --target-org prod --test-level RunLocalTestsThe destructive changes file uses the same format as package.xml, with the unwanted members listed under their type names. Use the post-destructive form so that the restored components deploy first and the references to the deleted component disappear before deletion runs.
What a rollback still cannot fix
A metadata rollback restores definitions. It does not restore records created or changed by the bad release, such as data a flow updated. It also cannot restore a deleted field's data. Deployed profile and permission set changes may need a separate review. Plan data recovery separately from the metadata rollback.
Prevention for change set teams
- Retrieve a backup of every component in the change set before you deploy.
- Validate the inbound change set first. If validation passes with the required tests, you can quick deploy it later without rerunning tests.
- Record the sandbox state or Git commit that matches production before the release.
Exercise
Your team deployed an inbound change set that overwrote five Apex classes and added a sixth. You have no backup, and the source sandbox now holds the new versions. Why can't you clone the change set to roll back, and what two things must you do to recover safely if an older Git commit holds the old versions?
Show answer
A cloned change set uploads whatever versions the source sandbox holds, which are the new, harmful ones. To recover, deploy the five old class versions from the Git commit with the CLI, and delete the sixth added class, either manually in Setup or with a destructive changes manifest. Remove any references to it first.