Sub topic 7
Pre versus post destructive changes
What this chapter covers
A destructive deployment can delete components before it deploys new ones, after it deploys them, or both. This chapter explains how to choose.
It assumes you can already write a destructive changes file (the previous chapter) and run sf project deploy start --manifest against a sandbox. It also assumes you know that a component can't be deleted while another component still refers to it.
Three files, one deployment
A Metadata API deployment can carry up to three kinds of manifest:
| File | When its deletions run | Notes |
|---|---|---|
| `package.xml` | Deployed in the main step | Required for every deployment. It can be empty of types. |
| `destructiveChangesPre.xml` | Before the main step | Deletes first, then deploys. |
| `destructiveChangesPost.xml` | After the main step | Deploys first, then deletes. |
The older destructiveChanges.xml file name behaves like the post file. If you use Metadata API zip packages directly, the file names decide the timing.
With the Salesforce CLI you choose the timing with a flag, not a file name:
--pre-destructive-changes <path>runs the deletions before the deploy.--post-destructive-changes <path>runs the deletions after the deploy.
Both flags take a path to a file in the usual package.xml format. You can name the files anything. This tutorial uses destructiveChangesPre.xml and destructiveChangesPost.xml so the timing is visible in the name.
All three kinds of manifest travel in one deployment request. The deploy runs as a single operation, so a failure in one step affects the others. That is the main reason the order matters.
The rule of thumb
Ask what the remaining metadata looks like at each step.
- Use a post file when something you are deploying removes the reference to the component you want to delete. The deploy fixes the dependency, then the deletion succeeds.
- Use a pre file when something you are deploying can't exist next to the component you want to delete. The deletion clears the way, then the deploy succeeds.
Most real deletions are post deletions. Pre deletions are the exception, and they are worth using only when you can name the conflict.
When to use a post file
Suppose AccountController calls LegacyAccountService. You are retiring the legacy class and have updated the controller to call AccountService instead.
If you delete LegacyAccountService first, the org still holds the old AccountController, which references it. The deletion fails with a dependency error.
If you deploy first, the controller is replaced with the new version, which no longer refers to the legacy class. After that, the deletion has nothing blocking it.
The manifest for the deploy step:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>AccountController</members>
<members>AccountService</members>
<name>ApexClass</name>
</types>
<version>62.0</version>
</Package>The post destructive file:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>LegacyAccountService</members>
<name>ApexClass</name>
</types>
</Package>The command:
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxThis is the pattern for removing a field that is used elsewhere. Deploy the layouts, flows, and classes that no longer mention the field, then delete the field in the post file.
When to use a pre file
A pre file helps when the deploy would collide with, or be blocked by, something that currently exists. These are the common cases:
- Same name, different shape. You want to replace a component with a new one that has the same API name but can't be changed in place. Deleting the old one first lets the deploy create the new one.
- A component that blocks a change you are deploying. For example, a deploy that changes a parent component fails because an old child component still depends on the previous shape of the parent. Removing the child first removes the obstacle.
- Cleaning up before a replacement lands. An old version of a component is deleted so that the deploy does not merge into stale configuration.
A pre file for the same-name case:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Account.Region__c</members>
<name>CustomField</name>
</types>
</Package>You run it with a dry run first:
sf project deploy start \
--manifest manifest/package.xml \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--dry-run \
--target-org my-sandboxA pre deletion of a custom field also deletes the data stored in it. Deleting a field to recreate it with a different type is a data loss decision. Export the data first if you need it.
Using both in one deployment
You can pass both flags in one command. A typical case is replacing a component and retiring a different one in the same release.
sf project deploy start \
--manifest manifest/package.xml \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxKeep each deletion in the file where it belongs. Don't list a component in both files. The second deletion would target something that is already gone.
A deletion-only deployment
Sometimes you only want to delete. The deployment still needs a manifest, so you pass an empty package.xml that has a version and no types:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>62.0</version>
</Package>With nothing to deploy, the main step does nothing, so pre and post produce the same result as far as order goes. Dependencies among the deleted components themselves still matter. If two components depend on each other, the deletions can fail even though nothing else in the org references them. Don't rely on the order of entries inside one file. If a dependency chain fails, split it across the pre and post files, or across two deployments.
What the flags require
Here is how the destructive flags relate to the other options.
| You pass | Requirement |
|---|---|
| `--pre-destructive-changes` or `--post-destructive-changes` | You must also pass `--manifest`. |
| `--dry-run` | Validates the whole deployment, including both destructive files, without saving anything. |
| `--source-dir` alone | Can't carry destructive files. Use `--manifest`. |
Two more rules apply to the contents of the destructive files:
- Wildcards aren't supported. Every member is listed by its exact name, such as
Account.OldRegion__c. - The
<version>element is optional in a destructive file. The version inpackage.xmlis the one used for the deployment.
Deleting from the local project too
A destructive deployment changes the org. It does not touch your local source files or your version control history. After the deployment succeeds, the deleted components may still sit in force-app. The next deploy of that directory would put them back.
Remove them from the project in the same commit as the manifests. Then the repository matches the org, and the destructive file remains as a record of what was deleted and when. The sf project delete source command does both the org and local steps in one go. That command is the subject of a later chapter.
Choosing: a checklist
Before each deletion, work through these questions:
- What references this component today?
- Does any component in this deploy remove that reference? If yes, use post.
- Will something in this deploy collide with the old component? If yes, use pre.
- Can the deletion succeed with no deploy at all? If yes, an empty manifest and either file is fine.
- Did a
--dry-runagainst a sandbox pass with the same flags you plan to use in production?
If a deletion fails in a dry run, don't switch from post to pre as a guess. A pre deletion sees the org before any of your changes, so it is more likely to hit a dependency, not less. Read the error to find out which component is still holding the reference.
Exercise
You are removing the Apex class OldPricing. The class PricingEngine in your project used to call it, and you have already edited PricingEngine so it no longer does. Which flag do you use for OldPricing, and why?
Show answer
Use --post-destructive-changes. The deploy replaces PricingEngine with the version that no longer references OldPricing. After that, the deletion has no blocking reference. With --pre-destructive-changes the org would still hold the old PricingEngine, so the deletion would fail with a dependency error.
Exercise
Write the command that validates, without saving, a deployment that uses manifest/package.xml, deletes the components in manifest/destructiveChangesPre.xml first, and targets the org alias my-sandbox.
Show answer
sf project deploy start --manifest manifest/package.xml --pre-destructive-changes manifest/destructiveChangesPre.xml --dry-run --target-org my-sandbox
Exercise
A teammate puts members with a wildcard in destructiveChangesPost.xml to remove every class whose name starts with Temp. What happens, and what should they do instead?
Show answer
Wildcards aren't supported in destructive changes files, so the deletion does not select the Temp classes. They should list each class by its exact name, for example one members element per class under the ApexClass type name.