Sub topic 8
Running a destructive deployment
This chapter assumes you can read a package.xml, have a Salesforce DX project with a force-app directory, and have the sf CLI authorized to a sandbox. It also assumes you know what a destructive changes file looks like. Here you will run one.
What the CLI does
A destructive deployment is a normal Metadata API deployment with an extra list of components to delete. The CLI sends three things:
- The
package.xmlmanifest, which lists components to add or update. - Optionally, a pre destructive file, whose deletions run before the manifest components are deployed.
- Optionally, a post destructive file, whose deletions run after.
The whole request succeeds or fails together. If a deletion fails, the deployment fails and nothing is saved.
Step 1: Prepare the files
Put the files together in manifest/. A deletion-only deployment still needs a manifest, so use a 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>Then write the destructive file. It uses the same format as package.xml, and each member must be the exact component name. Wildcards are not supported here.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>LegacyAccountService</members>
<name>ApexClass</name>
</types>
<types>
<members>Account.OldRegion__c</members>
<name>CustomField</name>
</types>
</Package>Save it as manifest/destructiveChangesPost.xml.
Step 2: Validate first
Add --dry-run to check the deployment without saving anything. The org evaluates the deletions as part of the check. This example uses a pre destructive file:
sf project deploy start \
--manifest manifest/package.xml \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--dry-run \
--target-org my-sandboxA dry run that passes means the deletions would have succeeded against the org's current state. A later deployment can still fail if someone changes the org in between.
Step 3: Deploy
Remove --dry-run to apply the change. This command deploys the manifest components and then deletes the post destructive components:
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxYou can pass both --pre-destructive-changes and --post-destructive-changes in one command. The destructive flags are used together with --manifest, so you can't use them with --source-dir or --metadata.
Choosing pre or post
The choice depends on what refers to what.
| Situation | Use |
|---|---|
| The deployed components stop using the component you delete (for example, a new class no longer calls the old one) | Post destructive |
| An existing component blocks something you are deploying (for example, a field you are replacing with a different type) | Pre destructive |
| The deleted component is not referenced by anything | Either one |
Post is the usual default. The new components replace the references first, so the deletion finds nothing depending on the target.
Reading the result
The CLI prints a table of deployed components and, if a deletion fails, a list of errors with the component name. Common messages and causes:
No ApexClass named: X found: the member name is misspelled, or the component is already gone.- A message that an entity is referenced by another component: something still depends on it. Update or remove the dependent component, or move the deletion to the other file (pre or post).
- A message that a field is used in a flow, layout, report, or Apex: the field can't be deleted until those references are changed.
Fix the error in your files, run --dry-run again, then deploy.
Putting it together
A safe sequence for a sandbox:
- Create
package.xmland the destructive file. - Run the command with
--dry-run. - Fix any errors and repeat until the dry run passes.
- Run the same command without
--dry-run. - Check the org in Setup to confirm the component is gone.
Exercise
You want to delete the Apex class LegacyAccountService from a sandbox and deploy nothing else. Which files do you need, and which command validates the change without saving it?
Show answer
You need a package.xml with only a <version> element (a deployment requires a manifest) and a destructive changes file listing LegacyAccountService under the ApexClass type. Validate with sf project deploy start --manifest manifest/package.xml --post-destructive-changes manifest/destructiveChangesPost.xml --dry-run --target-org my-sandbox. Pre destructive would also work, because nothing is deployed.
Exercise
A dry run fails because the field Account.OldRegion__c is still referenced by a new Apex class that you are deploying in the same package. Is the field named correctly, and does it belong in the pre or post destructive file?
Show answer
The name is correct, since fields are written as Object.FieldName. The problem is the order. If the new class is being changed to no longer reference the field, the deletion belongs in the post destructive file, so the updated class is deployed first. If the new class still references the field, you must edit the class before deleting the field.