Sub topic 4
Capturing a baseline before you deploy
A rollback needs something to roll back to. In Salesforce you build that target before the release, because the platform keeps no history of your metadata. This chapter covers how to record what production looks like before you change it, and where the recorded copy falls short.
This chapter assumes you can read a package.xml manifest and run sf project retrieve and sf project deploy commands. It also assumes basic Git (commits, tags, reverting a merge) and that you know the core metadata types: Apex classes, custom fields, flows, profiles.
Why the baseline must come from production
Salesforce does not keep earlier versions of your Apex classes, page layouts, or validation rules. Once a deployment succeeds, the previous definitions are gone from the org. A later chapter shows how to redeploy an earlier version, but that only works if you still have the files.
You have two sources for those files:
- Your Git repository. It holds what your team believes is in production.
- A retrieve from production. It holds what is actually in production.
These can differ. Someone may have made a hotfix in Setup. A past deployment may have skipped Git. An admin may have changed a layout by hand. If you roll back from a Git baseline that has drifted from production, you restore a state production never had.
The safest baseline is a retrieve from production taken just before the release, backed up in Git or in storage you control.
flowchart LRThe block above is a placeholder only and is not part of the chapter. The real diagram follows.
Step 1: Build a manifest that covers the release
The baseline only covers what you ask for. The simplest approach is to reuse the release manifest, since it names every component the deployment will touch.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>OrderService</members>
<members>OrderHelper</members>
<name>ApexClass</name>
</types>
<types>
<members>Order__c.Discount__c</members>
<name>CustomField</name>
</types>
<types>
<members>Order_Approval</members>
<name>Flow</name>
</types>
<version>62.0</version>
</Package>In this example, OrderService already exists in production and the release changes it. OrderHelper is new in this release.
If you list a component that production does not have, the retrieve reports it as not found. That warning is useful. It shows which components are new, and those are the ones you will later remove with a destructive changes file.
Step 2: Retrieve from production
Run the retrieve against production and send the output to a folder outside your normal source directories.
sf project retrieve start --manifest manifest/package.xml --target-org prod --output-dir backup/pre-releaseThree details matter here:
- Keep the folder out of your package directories. If
--output-dirpoints at a directory listed insfdx-project.json, the command fails. A top-levelbackup/folder that is not a package directory avoids this. - The result is source format. You can redeploy it later with the same CLI, without converting it.
- Retrieve is read-only. It changes nothing in the org, so it is safe to run at any time.
After the command finishes, check the output. List the folder and confirm each component type from your manifest is there. A retrieve can finish with warnings and still leave some components out. An empty or partial backup that nobody checked is the common way this step fails.
Step 3: Compare the backup with Git
Before you trust either copy, compare them. Check out the commit you are about to release from, then compare the retrieved files with the matching files in your repository. Any plain diff tool works, and the folder layouts may differ, so look at the structure of backup/pre-release first.
Differences usually fall into one of three groups:
| Difference | Likely cause | What to do |
|---|---|---|
| A component is in production but not in Git | Created in Setup or by an old deployment | Decide whether to add it to Git. If your release overwrites it, the backup is the only record. |
| A component differs in content | Manual edit in production, or a skipped deployment | Resolve this before deploying. Otherwise your release may overwrite someone's change without you knowing. |
| A component is in Git but not in production | Not yet released, or deleted from the org | Confirm it is intended. |
You do not have to remove every difference. You do have to know which ones exist, because each one changes what a rollback restores.
Step 4: Tag Git and keep the backup
A backup folder on one laptop is not a baseline the team can use. Make it findable in two ways.
First, tag the Git commit that matches production. Then store the backup folder somewhere shared. Many teams commit it to a separate branch or a separate repository, and others use an artifact store attached to the release record.
A small script keeps the steps in order and makes them repeatable:
#!/usr/bin/env bash
set -euo pipefail
ORG="prod"
RELEASE="2026-12-01"
BACKUP_DIR="backup/pre-release-${RELEASE}"
TAG="pre-release-${RELEASE}"
sf project retrieve start \
--manifest manifest/package.xml \
--target-org "${ORG}" \
--output-dir "${BACKUP_DIR}"
git tag -a "${TAG}" -m "Production baseline before release ${RELEASE}"
git push origin "${TAG}"
echo "Baseline stored in ${BACKUP_DIR} and tagged ${TAG}"set -e stops the script if the retrieve fails, so a failed retrieve never gets a tag. The tag name includes the date, so several releases can have separate baselines.
What the retrieve does not capture
A metadata backup is a copy of definitions. It has limits that you should know before you rely on it.
| Not captured | Why it matters |
|---|---|
| Record data | Metadata retrieves do not include rows in objects. A release that runs a data fix or a flow that updates records changes data the backup cannot restore. |
| Components you did not list | If a release has a side effect on something outside the manifest, the backup has no copy of it. |
| Components in managed packages | You cannot retrieve the code of a managed package. Only the version of the package can be restored, by reinstalling the earlier version. |
| Flow version history | A retrieve returns one version of a flow, not every saved version. |
| Some org settings and user-level assignments | Not every type is supported by the Metadata API. Check the metadata coverage report for the types you rely on. |
The data limit has the largest impact. A later chapter covers changes that a metadata redeploy cannot undo. If a release touches data, take a separate data export before the deployment, because the metadata baseline will not protect you.
Profiles and permission sets need extra care
Profiles are the most common source of a misleading backup. When you retrieve a profile through a manifest, the file contains only the permissions that relate to the other components in the same retrieve. It is not a full copy of the profile.
This has two consequences:
- If your release changes object or field permissions on a profile, include the related objects, fields, and Apex classes in the same manifest. Otherwise the retrieved profile file will not hold the permissions you need to restore.
- A restored profile can overwrite settings in production that you did not mean to touch, if the backup is narrower than the live profile.
Where you can, prefer permission sets over profile changes in releases. Permission set files are easier to reason about and to restore, because they change one grant at a time.
Timing: when to take the baseline
Take the baseline as close to the deployment as you can. A retrieve from Monday does not describe production on Friday if an admin made a change on Wednesday.
A practical order:
- Freeze changes in production for the release window, or agree on who may change it.
- Run the retrieve and the Git tag.
- Compare, resolve differences, and store the backup.
- Move on to validation, covered in the next chapter.
If there is a long gap between the baseline and the deployment, repeat the retrieve. It is cheap, and a stale baseline looks like a good one until you need it.
Common mistakes
- Retrieving only the changed files from Git. Your repository diff lists what you changed. It does not tell you what production held for those files.
- Writing the backup into a package directory. The command fails, or you mix the backup with live source.
- Skipping the check of the output. A retrieve with warnings can complete without every component.
- Keeping the backup only locally. The person who took it may be unavailable when you need it.
- Forgetting data. A metadata backup is silent about records.
Exercises
Exercise
Your Git main branch shows an Apex class OrderService with 120 lines. A retrieve from production just before the release returns a version with 135 lines. What should you do before deploying, and why?
Show answer
Treat the production copy as the real baseline and find out why they differ, for example a manual edit or a skipped deployment. Decide whether the extra lines should be merged into Git. If you deploy without resolving this, your release overwrites the 135-line version, and a rollback from Git would restore a version production never had.
Exercise
Your release manifest lists a new class OrderHelper. The pre-release retrieve prints a warning that OrderHelper cannot be found. Is this a problem?
Show answer
No. The class does not exist in production yet, so the warning is expected. It confirms that OrderHelper is new, which means a rollback must delete it with a destructive changes file, because there is no earlier version to restore.
Exercise
The release includes a flow that updates the Status__c field on existing orders. Does the pre-release metadata retrieve protect you if the flow updates the wrong records?
Show answer
No. A metadata retrieve holds definitions, not record data. To undo wrong updates you need a data export taken before the release.