Sub topic 12
Putting it together in a rollback runbook
This chapter assumes you know package.xml manifests, basic Git (commit, revert, branches), and how to deploy with Salesforce CLI or change sets. It combines the earlier recovery techniques into one written procedure that your team can follow under pressure.
Why write a runbook
Salesforce has no rollback button. Recovery is a set of separate actions: cancel, redeploy, delete, and manual repair. During an incident, people choose badly when they must remember all of them. A runbook is a short document that fixes the order of those actions and names who does each one. You write it before the release, not during the incident.
The decision path
The first question is always whether the deployment failed or succeeded. A failed deployment changes nothing in the org, because Salesforce rolls it back automatically. A successful deployment that causes problems needs your intervention.
Runbook sections
Give each release a runbook with these five sections.
- Baseline. Record the Git commit that matches production and the location of the backup retrieved before the release.
- Validation. Record the validation job ID and the test level used.
- Triggers. State what counts as a rollback condition, such as failing smoke tests or error rates above an agreed limit. Name the person who may call it.
- Steps. List the exact commands, in order, with the org alias filled in.
- Exceptions. List the changes that a metadata redeploy cannot reverse, and who handles each one.
Before the release
Take the backup and validate. Both commands come from earlier chapters.
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 RunLocalTestsStore the validation job ID in the runbook. If validation passes, a quick deploy can use that result and skip re-running tests during the release window. Quick deploy only works for a limited time after validation, so check the current limit in Salesforce documentation when you schedule.
During the release
If the release is still running and you must stop it, cancel it by job ID. Check the status first.
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prod
sf project deploy cancel --job-id 0AfXXXXXXXXXXXXXXX --target-org prodRolling back a successful release
Revert the release merge commit, then deploy the reverted source.
git revert -m 1 <merge-commit-sha>
sf project deploy start --manifest manifest/package.xml --target-org prod --test-level RunLocalTestsComponents that the release added are not removed by this redeploy. List them in a destructive changes file, prepared in advance when the release is built.
<?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>Then deploy the restored components and delete the added ones in one deployment.
sf project deploy start --manifest manifest/package.xml --post-destructive-changes destructiveChangesPost.xml --target-org prod --test-level RunLocalTestsUse a post-destructive file when restored components must exist before the deletion happens, for example when another component referenced the deleted one.
Match the change to the recovery
| Change in the release | Recovery in the runbook |
|---|---|
| Modified Apex class or field settings | Redeploy previous version from Git |
| New component | Destructive changes file |
| Flow or active configuration | Redeploy the old version and confirm which flow version is active |
| Change set release | Build an inverse change set manually, or deploy from source |
| Data changes, such as record updates | Restore from a data backup or fix with a data script. Metadata cannot do this |
Test the runbook
Run the whole procedure in a sandbox that mirrors production. Time it. A runbook that nobody has run often fails on small details, such as a wrong org alias or a missing destructive changes file.
After the incident
Record what was rolled back, how long it took, and what the report showed. Update the runbook with whatever surprised you.
Exercise
A release added the Apex class OrderHelper and modified the class OrderService. It passed deployment but breaks checkout. Which steps belong in the rollback, in order?
Show answer
Revert the release merge commit in Git, so OrderService returns to its previous source. Create a destructive changes file that lists OrderHelper as an ApexClass. Deploy with sf project deploy start, the manifest, and --post-destructive-changes, so the old OrderService is restored and OrderHelper is deleted in the same deployment. Then verify checkout and record the outcome.
Exercise
Name two items in a release that your runbook should list as exceptions, because a metadata redeploy cannot reverse them.
Show answer
Record data changes (such as records updated by a script or flow during the release) and stored values of a deleted custom field. Each needs a separate data restore or repair step with a named owner.