Sub topic 12
Putting It Together in a Rollback Runbook
This chapter assumes you already know the individual recovery techniques from earlier chapters. You should be comfortable with package.xml manifests, sf project deploy commands, and git revert. You should also know the difference between a deployment that fails and one that succeeds but causes harm. Here you combine those techniques into one document your team can follow under pressure.
Why a runbook matters
Salesforce has no rollback button. Recovery is a new deployment, and that deployment is subject to the same rules as the one that went wrong. It needs tests, it takes time, and it can fail. If you work out the recovery during an incident, you will make decisions while users are affected and people are stressed.
A rollback runbook is a written, rehearsed procedure that answers these questions before the release starts:
- Who decides to roll back, and by what criteria?
- Which backup and which Git commit are the restore point?
- Which commands run, in which order, against which org?
- What can the procedure not undo, and who handles that?
- How do you confirm the org is back to a known good state?
Anatomy of a runbook
A useful runbook has the same sections for every release. The table lists them and the earlier concept each one relies on.
| Section | What it contains | Concept it relies on |
|---|---|---|
| Release identity | Release name, merge commit SHA, deployment job ID, target org alias | Source control baseline |
| Restore point | Path to the pre-release retrieve and the Git commit of the previous release | Pre-deployment backup |
| Decision criteria | Symptoms that trigger a rollback, and who may call it | Failed versus bad success |
| Recovery steps | Ordered commands for each path | Cancel, redeploy, destructive changes |
| Manual steps | Flow versions, permission changes, data repair | Flows and irreversible changes |
| Verification | Smoke tests and checks that prove the org is restored | Deployment report |
| Communication | Who is told, when, and through which channel | Not covered by tooling |
Keep the runbook in the same repository as the release, or link to it from the release ticket. A runbook stored somewhere nobody looks will not be used.
Choosing the recovery path
The first decision during an incident is which path to follow. The deployment status tells you.
A failed deployment is atomic. Salesforce commits nothing from it, so there is nothing to roll back. The recovery is to read the failure report and fix the cause. Only a successful deployment that did harm, or a cancellation that arrived too late, needs the full rollback path.
Before the release: the preparation block
Everything the rollback needs must exist before you click deploy. Put these steps into the runbook as a checklist that someone signs off.
- Tag or note the Git commit of the last known good release.
- Retrieve the current production versions of every component in the release manifest.
- Validate the release against production with the same test level you will use for the real deployment.
- Write down the validation job ID so you can quick deploy it.
- Prepare the destructive changes file for any component the release adds.
The retrieve and validate steps look like this:
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 RunLocalTestsThe retrieve covers only components that already exist in production. A component the release adds has no pre-release version, so it appears in the destructive changes file instead of the backup.
Preparing the destructive file in advance is the step teams skip most often. Writing it before the release lets a second person review it. It is also hard to write correctly at 2 a.m. when you are not sure which components the release created.
During the release: the monitoring block
The runbook should name one person who watches the deployment and has authority to cancel it. A release can run for a long time when it includes many tests. A deployment that is clearly going wrong, for example because a production dependency changed mid-release, is cheaper to cancel than to repair afterward.
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prod
sf project deploy cancel --job-id 0AfXXXXXXXXXXXXXXX --target-org prodAlways run the report after a cancel. A cancel request is not instant, and a deployment that is already in its final commit phase may complete anyway. The report shows the final status, and the runbook should say not to continue until that status is confirmed.
After the release: the rollback block
When the release has succeeded and caused harm, the rollback has three parts that run in a fixed order.
Restore the previous versions
Revert the release merge commit, then deploy the result. The -m 1 option tells Git to treat the first parent, the main branch, as the line to keep.
git revert -m 1 <merge-commit-sha>
sf project deploy start --manifest manifest/package.xml --target-org prod --test-level RunLocalTestsThe manifest must list every component the release changed. A component missing from the manifest is not redeployed and keeps its new, harmful version.
Delete what the release added
Reverting source removes added files from your repository, but deploying a manifest never deletes anything from the org. Components created by the release remain in production until a destructive changes file removes them.
<?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>Deploy the restore and the deletion together, so the org never sits in a half-restored state:
sf project deploy start --manifest manifest/package.xml --post-destructive-changes destructiveChangesPost.xml --target-org prod --test-level RunLocalTestsA post-destructive file deletes after the rest of the deployment succeeds. That is the right choice when restored components no longer reference the deleted ones. Use a pre-destructive file when the deletion must happen first, for example when an old component conflicts with a new one.
Handle what metadata cannot restore
Some changes survive a metadata redeploy. The runbook needs a short list of these for the release, with an owner for each:
- Records inserted, updated, or deleted by the release's data scripts or by flows that ran while it was live.
- Field type changes that lost data when they were applied.
- Active flow versions, which need to be re-activated manually if the redeploy does not restore the active version.
- Permission or profile assignments made by hand after the release.
For each item, the runbook should say whether it has a repair procedure, such as a data backup to reload from, or whether the team accepts the loss.
A script for the mechanical steps
Commands you run during an incident should not be typed from memory. Wrap the mechanical steps in a script that stops at the first error, and keep it next to the runbook.
#!/usr/bin/env bash
set -euo pipefail
ORG="${ORG:-prod}"
MERGE_SHA="${1:?Usage: rollback.sh <merge-commit-sha>}"
echo "Reverting release commit ${MERGE_SHA}"
git revert -m 1 --no-edit "${MERGE_SHA}"
echo "Deploying restored source and deleting added components to ${ORG}"
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes destructiveChangesPost.xml \
--target-org "${ORG}" \
--test-level RunLocalTests \
--wait 90The script requires the commit SHA as an argument, so it fails loudly instead of reverting the wrong commit. It does not push the revert. A person should review the revert commit before it reaches the main branch.
Verification and closing out
A rollback is not finished when the deployment reports success. The runbook should end with checks that someone runs and records:
- The deploy report for the rollback shows
Succeededand zero component or test failures. - A smoke test of the business process that broke now works.
- The added components, such as
OrderHelper, no longer exist in Setup. - The org's Git baseline matches the repository's main branch.
- The affected users have been told the incident is resolved.
After the incident, hold a short review. Update the runbook with whatever surprised you. A runbook that has never changed after a real incident is probably not being used.
Rehearse it
A rollback you have never run has unknown failure modes. Test it in a full-copy or partial-copy sandbox that mirrors production. Deploy a harmless release that adds a class, a field, and a flow version, then roll it back using only the runbook. Time each step. The rehearsal will show missing manifest entries, test classes that fail after the revert, and steps that depend on knowledge only one person has.
Exercise
A release added the Apex class OrderHelper and modified the existing class OrderService. The release succeeded, but orders now fail. Using the runbook structure, list the files you need and the order of actions to roll back.
Show answer
Files needed: the pre-release backup or the Git baseline for OrderService, a manifest listing OrderService, and a destructive changes file listing OrderHelper as an ApexClass. Actions: (1) confirm via the deploy report that the release succeeded and is not still running, so this is a rollback and not a cancel; (2) revert the release merge commit in Git; (3) deploy the reverted source with the manifest and the post-destructive changes file in a single deployment with a test level that satisfies production rules; (4) check the report for zero failures; (5) verify that OrderService is back to the old version and OrderHelper no longer exists; (6) review any data the failed orders touched, because the metadata rollback does not repair records.
Exercise
Why should the destructive changes file be written and reviewed before the release rather than during the incident?
Show answer
The only reliable list of components the release adds is available before it ships, and under incident pressure it is easy to miss one or list the wrong one. Preparing the file in advance lets a second person review it and means the rollback needs no new decisions about what to delete.