Sub topic 13
Deleting components with destructive changes
This chapter assumes you can retrieve and deploy with sf project deploy start, you know what a package.xml manifest is, and you have a sandbox where you can safely test deletions. Deploys add and update components. They never remove anything from the org. This chapter covers how to delete components.
Why a normal deploy never deletes
If you remove OldHelper.cls from your project and run sf project deploy start --source-dir force-app, the org keeps OldHelper. The deploy only sends files that exist locally, so a missing file means nothing was sent.
The opposite is also true. Deleting a component in the org does not remove your local copy. The two sides stay out of sync until you delete explicitly on each side.
You have three ways to delete from an org:
| Method | Best for | Deletes locally too? |
|---|---|---|
| Destructive changes manifest passed to `sf project deploy start` | Releases, CI, production | No |
| `sf project delete source` | Quick cleanup in a sandbox | Yes |
| Deleting a local file in a source-tracked org, then deploying | Scratch orgs and tracked sandboxes | Already deleted |
This chapter focuses on the first method, because it is the one you can review, version, and replay against several orgs.
The destructive changes manifest
A destructive changes file uses the same XML format as package.xml. It lists the components to delete. Two rules differ from a normal manifest:
- Every member must be named. Don't use the
*wildcard. - It is always used together with a regular manifest, which can list components to deploy or can be empty.
Here is a file that deletes one Apex class and one custom field:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>OldHelper</members>
<name>ApexClass</name>
</types>
<types>
<members>Invoice__c.Legacy_Notes__c</members>
<name>CustomField</name>
</types>
<version>60.0</version>
</Package>Child components such as custom fields use the Object.Field form. Save the file as manifest/destructiveChangesPost.xml. The name is a convention, and the CLI uses whatever path you pass to the flag.
Pre and post: choosing when deletion happens
sf project deploy start has two flags for destructive files:
--pre-destructive-changesdeletes components before the deploy runs.--post-destructive-changesdeletes components after the deploy succeeds.
The order matters because Salesforce refuses to delete a component that something else still references.
Since the chapter's diagram must use the required fence, here is the sequence as a diagram:
Use these rules to choose:
- Post is the usual choice. Say you are removing
OldHelperand also editingAccountService, which called it. The newAccountServicemust go in first, so that nothing referencesOldHelperwhen it is deleted. - Pre is for cases where the old component blocks the new one. For example, you may need to delete a class before deploying a different one that takes over its name or a conflicting definition.
If you use neither a pre nor a post file but need the older behavior, Metadata API treats a file named destructiveChanges.xml as a post-deletion file. With the sf command, pass the explicit flag so the intent is visible in your scripts.
Running a destructive deploy
Pass a normal manifest plus the destructive file:
sf project deploy start --manifest manifest/package.xml --post-destructive-changes manifest/destructiveChangesPost.xml --target-org my-sandboxIf you only want to delete, the regular manifest can contain no types. It still needs the version element:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>60.0</version>
</Package>Save that as manifest/empty-package.xml and use it as the --manifest value. Then run the deploy:
sf project deploy start --manifest manifest/empty-package.xml --post-destructive-changes manifest/destructiveChangesPost.xml --target-org my-sandbox --dry-runThe --dry-run flag asks the org to check the whole job without saving it. Use it on every destructive deploy, because it exposes dependency errors before anything is removed. When the dry run is clean, run the command again without --dry-run.
Combining deploys and deletions
A release often needs both a change and a removal. You can pass both destructive flags in one command:
sf project deploy start --manifest manifest/package.xml --pre-destructive-changes manifest/destructiveChangesPre.xml --post-destructive-changes manifest/destructiveChangesPost.xml --target-org my-sandbox --test-level RunLocalTestsThe deploy is a single job. If the main deploy fails, the post deletions do not happen. Check the job status with sf project deploy report --use-most-recent, as you did in the earlier chapter on deploy status.
Deleting with `sf project delete source`
For a sandbox cleanup, sf project delete source removes the component from the org and from your local project in one step:
sf project delete source --metadata ApexClass:OldHelper --target-org my-sandboxThe command shows you the components and asks for confirmation. It also accepts --source-dir to delete everything under a directory, and --check-only to validate the delete without applying it. It is quick, but nothing records the deletion in a reviewable file. If you delete this way, remember to apply the same deletion to production through a destructive manifest.
Source tracking and deletions
In scratch orgs and sandboxes with source tracking enabled, the CLI notices when you delete a local file. A plain sf project deploy start with no source selection then sends the deletion to the org. This is convenient for development. It is also a reason to read the preview first:
sf project deploy preview --target-org my-sandboxThe preview lists deletions as well as additions and changes, so you can see what the next deploy would remove. In an org without source tracking, deleting a local file does nothing to the org.
Common errors and gotchas
- The component is still referenced. The error reads like "Cannot delete ... in use by ...". Find the referencing component, update it in the same deploy, and move the deletion to the post file. Layouts, profiles, flows, and Apex code are frequent culprits.
- The component does not exist. Deleting something that is already gone is normally reported as a warning, not a failure. Read the warnings anyway, because a typo in a member name looks the same as an already-deleted component.
- A wildcard in the member list. Name each component.
- The delete works in the sandbox but fails in production. Production may hold references that the sandbox lacks, such as data, reports, or a different set of packages. Run
--dry-runagainst production before the release window. - Tests run on production. A deploy to production that includes Apex deletions follows production test rules. Pick a test level on purpose, and validate first as described in the chapter on validate and quick deploy.
- Deleted fields and data. Deleting a custom field removes its data. Salesforce keeps deleted custom fields for a period so you can restore them, but don't rely on this as a backup.
Putting it into a repeatable flow
- Remove the local component, or list it in the destructive file.
- Update every component that referenced it, and list those in
manifest/package.xml. - Run the deploy with
--dry-runand the intended test level. - Fix reference errors, then repeat the dry run until it passes.
- Run the real deploy, or validate and quick-deploy for production.
- Delete the local files if the component is still in your project, and commit.
Exercise
You are deleting the Apex class OldHelper, and AccountService currently calls it. You have updated AccountService locally so it no longer does. Which destructive flag do you use, why, and which command lets you check the result before anything is deleted?
Show answer
Use --post-destructive-changes, and include AccountService in the manifest. The deploy updates AccountService first, so nothing references OldHelper when the deletion runs. A pre-destructive deletion would fail because AccountService would still reference OldHelper at that moment. Add --dry-run to the sf project deploy start command to check the whole job without saving changes.
Exercise
A teammate deletes Legacy.cls from their local project and runs sf project deploy start --source-dir force-app --target-org production. Is the class removed from production? How would you remove it?
Show answer
No. A deploy to production sends only files that exist locally, and production has no source tracking, so the deletion is never sent. List Legacy under ApexClass in a destructive changes file, then run sf project deploy start with a manifest and --post-destructive-changes pointing to that file. Run it with --dry-run or validate first.