How to Roll Back a Salesforce Deployment (There Is No Undo Button)
Salesforce has no one-click rollback. A failed deployment saves nothing, but a successful one stays. To revert it you redeploy the earlier version you captured beforehand. Here is what Salesforce does and does not undo, how to prepare, and how to revert.
Short answer: Salesforce has no one-click rollback. A deployment that fails is rolled back for you: by default nothing in the package is saved. A deployment that succeeds stays, and Salesforce does not keep the previous version of what it overwrote. To revert a bad deployment you redeploy the earlier version of the same components, which means you must have captured it before you deployed. Rollback is a preparation task, not a button.
What Salesforce rolls back, and what it does not
- A failed deployment. Metadata API deployments are all-or-nothing by default. If one component fails or one required test fails, none of the components in that package are saved, so there is nothing to undo. Production deployments always behave this way.
- A successful deployment. No undo. The Deployment Status page in Setup shows what was deployed and when, but it has no revert action. Change sets have no undo either.
- Data. A metadata deployment never touches records. If a Flow, trigger or Apex class changed data while it was live, redeploying the old metadata does not put the data back.
- Deleted components. Deleting a component through a destructive change is not the same as rolling one back. Some deleted components, such as custom fields, go to a recoverable state for a limited time and can be restored from Setup. Others cannot. Check Salesforce Help for each component type before you rely on it.
The practical consequence: after a successful deploy, going back means deploying forward. You push the old version of the components over the new one.
Step 1: Capture the before-state, every time
Before any production deployment, retrieve the current production version of every component the package will change. With the Salesforce CLI:
`sf project retrieve start --manifest package.xml --target-org production --output-dir backup/`
Use the same manifest as the deployment, so the backup covers exactly what you are about to overwrite. Commit it to version control, or at least store it somewhere that is not the org. A few cautions:
- Retrieve from production, not from a sandbox. Sandboxes drift. A sandbox copy is not the version that is live today.
- Profiles and permission sets need care. A retrieve returns only the parts of a profile that relate to the other components in the same retrieve, so a partial retrieve gives you a partial backup. Retrieve the profile together with the objects, fields and apps it references.
- Back up data separately. If the release includes data changes, a Flow that updates records, or a destructive change that deletes a field, the metadata backup does not cover the data. Take a data export first. Salesforce's Data Export Service and its Backup and Restore product are the platform options.
Step 2: Validate first, then Quick Deploy
A validation runs the full deployment, including tests, against the target org and saves nothing. If it passes, you can run a Quick Deploy of that validation within 10 days, without running the tests again.
`sf project deploy validate --manifest package.xml --target-org production --test-level RunLocalTests`
`sf project deploy quick --job-id --target-org production`
Validation is your best defence because it catches the failures that would otherwise happen at deploy time, and it does so while the cost is still zero. It does not catch behaviour changes. A package can deploy cleanly and still break a process, so test the behaviour in a sandbox too.
Step 3: Decide whether you need to roll back at all
Rolling back is a second production deployment, with its own risk. Before you do it, check the cheaper options:
- The deployment failed. Nothing was saved. Fix the package and deploy again; no rollback is needed.
- A Flow misbehaves. Each deployment of a Flow creates a new version. Open the Flow in Setup and activate the previous version. This is the fastest revert Salesforce offers, and it needs no deployment.
- A feature is switched on by one setting. Turn the setting or the permission off while you work out the fix.
- Only one component is wrong. Redeploy only that component's previous version rather than the whole package.
Step 4: Roll back by redeploying the previous version
If the deployment succeeded and you need to return to the earlier state:
- Stop other deployments to the org so you are not rolling back on a moving target.
- Take a fresh retrieve of production, so you have a record of the broken state as well.
- Build a rollback package from the before-state you captured in Step 1: the same components, at their old versions.
- Handle the components the original deployment added. Redeploying the old versions does not remove them. Remove them with a destructive change (a `destructiveChanges.xml` file, or the `--post-destructive-changes` option of the deploy command). Be careful here: deleting a field deletes its data.
- Validate the rollback package against production, then deploy it. Apex classes and triggers still need their tests to pass, as with any production deployment.
- Confirm the result by reading the org, not the deploy status: open the components, run the affected process, and check the data.
If you deployed by change set, the process is the same. Change sets cannot delete components, so additions need a manual cleanup or a deployment through the Metadata API.
Step 5: Make the next rollback easier
- Keep production metadata in version control so the previous version is one revert away.
- Deploy small, frequent packages. A rollback of three components is a short job. A rollback of three hundred is an incident.
- Write the rollback plan into the release plan before the deployment, not after it.
- Keep a record of who deployed what and when. If a problem appears a week later, you need to know which release to point at.
A quick read on how exposed your org is to a bad release is useful before you start. The free health check needs no account and no sales call: Run the free health check →
FAQ
Can you undo a Salesforce deployment?
Not with a button. A failed deployment saves nothing, so there is nothing to undo. A successful one stays until you deploy the earlier version of the components over it. That is why the before-state has to be captured in advance.
Does a failed Salesforce deployment roll back automatically?
Yes. By default a deployment is all-or-nothing: if any component or required test fails, none of the package is saved. Production deployments always work this way.
How do I roll back a change set?
Change sets have no rollback. Build a new change set (or a Metadata API package) from the previous versions of the same components, which you retrieved before the original deployment, and deploy it. Change sets cannot delete components, so remove anything the original deployment added by hand.
Does rolling back metadata restore my data?
No. A metadata deployment never changes records, and redeploying old metadata does not revert data changes made while the new version was live. Back up data separately.
How long does Quick Deploy stay valid?
A successful validation can be Quick Deployed for 10 days.
Can I recover a deleted custom field?
Salesforce keeps some deleted components, including custom fields, in a recoverable state for a limited time. Restore the component from Setup rather than deploying it, so the data comes back with it. Check Salesforce Help for the current window and for each component type.
Sources
- Salesforce Help: Deploy Changes With Change Sets, and Deploy Metadata From a Sandbox to Production
- Salesforce Metadata API Developer Guide: Deploying and Retrieving Metadata, and Deleting Components From an Organization
- Salesforce CLI Command Reference: `project deploy validate`, `project deploy quick`, `project retrieve start`
- Salesforce Help: Delete Custom Fields, and Manage Flow Versions
Troubleshooting a deployment that failed instead? Start with Salesforce deployment failures: causes and fixes.