Sub topic 13
Deleting components with destructive changes
This chapter assumes you can retrieve and deploy with a package.xml manifest and that you have an authorized default org. Deleting a file from your local project does not delete the component from the org. To remove components from an org, you deploy a destructive changes manifest.
How destructive changes work
A destructive changes file is an XML manifest that has the same shape as package.xml. It lists the components to delete instead of the ones to deploy. You pass it to the deploy command, together with the regular manifest. The regular manifest can be nearly empty if you only want to delete.
Two flags control when the deletion happens relative to the rest of the deployment:
| Flag | When components are deleted | Typical use |
|---|---|---|
| `--pre-destructive-changes` | Before the deploy | Remove something that conflicts with what you are about to deploy |
| `--post-destructive-changes` | After the deploy | Remove something that other components referenced until this deploy updated them |
Both flags need --manifest. You cannot combine them with --source-dir.
Writing the destructive manifest
Create manifest/destructiveChangesPost.xml. Each member is named explicitly.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>OldAccountHelper</members>
<name>ApexClass</name>
</types>
<types>
<members>Legacy_Note__c</members>
<name>CustomObject</name>
</types>
<version>60.0</version>
</Package>Wildcards such as * do not work in a destructive file. List each component by its full name.
Running the deployment
Deploy the manifest, then delete the listed components:
sf project deploy start --manifest manifest/package.xml --post-destructive-changes manifest/destructiveChangesPost.xml --target-org my-sandboxAdd --dry-run first to see whether the deletion would succeed without changing the org. Run this against a sandbox before you try it against production.
Choosing pre or post
Most deletions use post. Suppose a class Reporter uses a field you want to delete. You deploy the updated Reporter first, so it no longer references the field. Then you delete the field. A pre-destructive delete would fail because Reporter still depends on the field at that moment.
Use pre when the deploy would otherwise collide with the old component. An example is replacing a component with a new one that has the same name but a different type or structure.
Common errors
- Dependency failures. The deletion is rejected because another component still references the target. Update or remove the dependent component in the same deploy, and use the post flag.
- Component not found. The member name has a typo or the component is already gone. Check the exact API name, including the
__csuffix. - Wildcards in the file. They are not supported. Replace
*with explicit names. - Deleting locally and expecting the org to follow. Removing a file from
force-appchanges only your project. Add the component to a destructive manifest.
Exercise
You deleted the field Priority__c from your project, and the class TaskService used it until you edited the class in the same commit. Which flag do you use for the destructive file, and why?
Show answer
Use --post-destructive-changes. The deploy first updates TaskService so it no longer references Priority__c, and the field is deleted afterward. A pre-destructive delete would fail because TaskService would still depend on the field.