Sub topic 8
Running a destructive deployment
This chapter assumes you can read a package.xml and write a destructive changes file, which earlier chapters covered. It also assumes the Salesforce CLI (sf) is authorized to a sandbox and that you have a Salesforce DX project with a force-app directory. Here you run the deletion: which flags to pass, what order the Metadata API processes things in, and how to check the result.
What a destructive deployment is
A destructive deployment is an ordinary Metadata API deployment that also carries one or more destructive changes files. The deployment can contain:
- A
package.xmlthat lists components to create or update. - A pre destructive file that lists components to delete before the deploy.
- A post destructive file that lists components to delete after the deploy.
The CLI zips these together with any source components and sends them as one request. The server processes the request in a fixed order.
A failure at any step fails the deployment. In production the deployment is all-or-nothing, so a failed run does not leave a half-finished state.
The CLI flags
The command is sf project deploy start. Three flags control destructive behavior.
| Flag | Purpose |
|---|---|
| `--manifest` | Path to the `package.xml` that lists components to deploy. |
| `--pre-destructive-changes` | Path to a file listing components to delete before the deploy. |
| `--post-destructive-changes` | Path to a file listing components to delete after the deploy. |
Both destructive flags need --manifest alongside them. The CLI builds the deployment from the manifest and attaches the destructive files to it. If you pass a destructive flag without --manifest, the command fails because it has no manifest to attach them to.
A worked example
Suppose AccountService calls a class named LegacyAccountService. You have refactored AccountService so it no longer uses the legacy class, and you want to delete LegacyAccountService and the field Account.OldRegion__c.
Start with the manifest. It lists the class you are updating, so the new version goes out in the same deployment.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>AccountService</members>
<members>AccountServiceTest</members>
<name>ApexClass</name>
</types>
<version>62.0</version>
</Package>Next, the post destructive file. It names each component to delete by its exact name. Wildcards are not allowed 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>Then run the deployment against a sandbox.
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxThe server first deploys the new AccountService, which no longer refers to the legacy class. Then it deletes LegacyAccountService and Account.OldRegion__c. The order is what makes the deletion succeed. If the legacy class were deleted first, the old AccountService would still reference it and the deletion would be blocked.
Choosing pre or post
The choice depends on whether something you are deploying stands in the way of the deletion, or the deletion stands in the way of something you are deploying.
| Situation | Use |
|---|---|
| The new code no longer references the component you are deleting. | Post destructive. Deploy the new code first, then delete. |
| An existing component blocks the deploy, for example one that holds a name the incoming component needs. | Pre destructive. Delete first, then deploy. |
| The deploy and the deletions are unrelated. | Post destructive is the safer default. |
Post is the safer default because the replacement code is already in place when the deletion happens. The usual refactoring pattern is to stop using a component in one deployment and delete it in the same deployment's post file.
You can pass both flags in one command when a change needs both.
sf project deploy start \
--manifest manifest/package.xml \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxDeleting without deploying anything
Sometimes you only want to delete. The deployment still needs a manifest, so use a package.xml with no types and only a version.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>62.0</version>
</Package>Pass it with --manifest as usual.
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxFor a delete-only run, pre and post behave almost identically, because there is nothing between them. Post is still the convention.
Validating with a dry run
Add --dry-run to validate the deployment without saving anything to the org. The server processes the whole request, including the deletions, and reports what would happen. A dry run is your main safety net.
sf project deploy start \
--manifest manifest/package.xml \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--dry-run \
--target-org my-sandboxA dry run catches wrong member names, missing components, and dependency errors. It does not catch every runtime problem. For example, it cannot tell you that a Flow you forgot about will break in production, unless that Flow's reference blocks the deletion in the first place. Treat a passing dry run as proof that the API will accept the request, not as proof that the change is safe for your users.
Tests and production
Deployments to production that include Apex components run Apex tests, and the same rules can apply when you delete Apex classes. State the test level explicitly so the behavior is predictable.
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--test-level RunLocalTests \
--dry-run \
--target-org my-productionIf the validation passes, you can deploy the validated job without running the tests again. Use the job ID printed by the dry run.
sf project deploy quick \
--job-id 0AfXXXXXXXXXXXXXXX \
--target-org my-productionQuick deploy only works for a validation that has passed tests, and Salesforce limits how long a validation stays eligible. Check the current window in the Salesforce documentation before you rely on it for a scheduled release.
Reading the result
By default the CLI waits for the deployment and prints a table of components with their status. A successful run lists the deleted components with a state of Deleted. A failed run lists each failing component with a problem message. To check on a deployment that is still running, or one you started earlier, use the report command.
sf project deploy report --use-most-recentAdd --json to any deploy command when a script needs to parse the result. The exit code is non-zero when the deployment fails, so a CI job can stop on it.
What the deployment does not do
A destructive deployment changes the org only. It does not touch your local project. After a successful run, LegacyAccountService.cls and its metadata file are still in force-app. If you leave them there, the next deployment that includes that directory recreates the class in the org.
After a successful deletion, remove the files from source control as well.
- Delete the files from
force-app, for example the.clsand.cls-meta.xmlfiles. - Commit the removal together with the destructive changes file so the history shows why.
- Deploy to the next org in your path using the same destructive file.
Each org needs its own destructive deployment. Deleting a component from a sandbox does not delete it from production.
Common mistakes
- Using
*in a destructive file. Wildcards are valid in a retrieve or deploy manifest, but a destructive file must list each component by exact name. - Forgetting
--manifest. The destructive flags attach files to a manifest-based deployment, so they need one. - Wrong member format. A custom field is
Account.OldRegion__c, notOldRegion__c. The object name is part of the member name. - Picking pre when you meant post. Deleting before the replacement code is deployed leaves old references in place and the deletion fails.
- Skipping the dry run. The same command with
--dry-runcosts a minute and shows the failure before it matters. - Leaving the source files in the project. The component returns on the next deploy.
Exercise
You refactored the Apex class OrderService so it no longer calls OldPricing. You want to deploy the new OrderService and delete OldPricing in one command. Which destructive flag should you use, and why? Write the command, assuming your manifest is manifest/package.xml, your destructive file is manifest/destructiveChangesPost.xml, and your org alias is my-sandbox. Include a flag that validates without saving.
Show answer
Use --post-destructive-changes. The new OrderService must be deployed first so nothing references OldPricing when it is deleted. The command is: sf project deploy start --manifest manifest/package.xml --post-destructive-changes manifest/destructiveChangesPost.xml --dry-run --target-org my-sandbox. After the dry run passes, run it again without --dry-run.
Exercise
A teammate's destructive deployment succeeds in the sandbox. A week later, OldPricing is back in the sandbox after another teammate deploys the force-app directory. What was missed, and what should be done?
Show answer
The deployment deleted the class from the org only, and the source files stayed in force-app. The next deploy of that directory recreated the class. Delete OldPricing.cls and OldPricing.cls-meta.xml from the project, commit the removal with the destructive changes file, and then delete the class from the sandbox again if it was recreated.