Sub topic 6
Writing a destructive changes file
This chapter assumes you can read package.xml, know the type and member naming rules from the earlier chapters, and have a Salesforce DX project with the sf CLI authorized to a sandbox. Here you will write the file that tells the Metadata API which components to delete. The next two chapters cover when the deletions run and how to run them.
What a destructive changes file is
A destructive changes file is an XML file that lists components to remove from an org. It uses the same format as package.xml: a Package root element, one types block per metadata type, and one members line per component. The difference is in the intent. A package.xml names components to retrieve or deploy. A destructive changes file names components to delete.
A deletion deploy needs two files:
- A manifest (
package.xml) that names whatever you are deploying. It can be empty of types. - One or more destructive changes files that name what to delete.
The Metadata API looks for these file names inside a deployment:
| File name | When the deletions run |
|---|---|
| `destructiveChangesPre.xml` | Before the rest of the deployment is saved |
| `destructiveChanges.xml` | After the rest of the deployment (same timing as Post) |
| `destructiveChangesPost.xml` | After the rest of the deployment |
With the Salesforce CLI you can name the files anything you like, because you pass each path explicitly with --pre-destructive-changes or --post-destructive-changes. The conventional names above keep a team's projects consistent, so use them.
The minimal file
Here is a destructive changes 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>LegacyAccountService</members>
<name>ApexClass</name>
</types>
<types>
<members>Account.OldRegion__c</members>
<name>CustomField</name>
</types>
</Package>Each part has a job:
Packagewith thehttp://soap.sforce.com/2006/04/metadatanamespace is the required root. A missing or misspelled namespace makes the Metadata API reject the file.typesgroups all deletions of one metadata type.membersholds the exact API name of one component to delete.nameholds the metadata type name, such asApexClassorCustomField.
This example has no version element. The accompanying package.xml supplies the API version for the deployment, so the destructive file does not need one. Adding one does no harm.
Names must match exactly
A destructive changes file does not search for things. It deletes by exact name, and the name is the same one you would write in a package.xml member. The wildcard * is not supported here. In a retrieve manifest, <members>*</members> means "every component of this type". In a destructive file it would be a request to delete everything of that type, and the Metadata API does not allow it. You must list each component.
The common naming patterns are below.
| Component | Type name | Member to write |
|---|---|---|
| Apex class | `ApexClass` | `LegacyAccountService` |
| Apex trigger | `ApexTrigger` | `AccountTrigger` |
| Custom object | `CustomObject` | `Invoice__c` |
| Custom field | `CustomField` | `Account.OldRegion__c` |
| Validation rule | `ValidationRule` | `Account.RequireRegion` |
| Lightning web component | `LightningComponentBundle` | `accountCard` |
| Page layout | `Layout` | `Account-Account Layout` |
| Report in a folder | `Report` | `SalesReports/PipelineByRegion` |
| Report folder | `Report` | `SalesReports` |
Some details behind that table:
- Fields and other child components use
ObjectName.ComponentName. The object name comes first, then a dot. A bareOldRegion__cfails because the Metadata API cannot tell which object owns it. - Custom objects and fields keep their suffix. Write
Invoice__c, notInvoice. The same goes for__mdton custom metadata types and__eon platform events. - Folder-based types such as reports, dashboards, documents, and email templates use
FolderName/ComponentName. To delete the folder itself, list just the folder name under the same type. - Layouts use
ObjectName-Layout Name, with a hyphen between the two. If the layout name contains special characters, the file name in source is URL-encoded (for example%20for a space), and you should check how the name appears in your project'slayoutsfolder. - Names are case-sensitive in practice. Copy the name from the source file name or from a retrieve result instead of typing it from memory.
Standard components are a special case. You cannot delete standard fields such as Account.Name, and you cannot delete standard objects. The deployment fails if you list them.
Getting the names right the first time
Typing member names by hand is where most mistakes start. These approaches avoid typos:
- Read the names off your local source. A file
force-app/main/default/classes/LegacyAccountService.clsmeans the member isLegacyAccountService. A fileforce-app/main/default/objects/Account/fields/OldRegion__c.field-meta.xmlmeans the member isAccount.OldRegion__c. - Generate a manifest from the folder you plan to delete, then copy its
typesblocks into the destructive file:
sf project generate manifest \
--source-dir force-app/main/default/classes/LegacyAccountService.cls \
--name manifest/to-delete.xml- Check the org. Run
sf project retrieve start --manifest manifest/to-delete.xml --target-org my-sandbox. If the retrieve returns the component, the name is valid in that org.
The second approach gives you correctly formed types, members, and name lines. The only edit needed is removing the version line if you prefer a cleaner file.
Building the file step by step
The decision at step D is the subject of the next chapter. For now, treat it as a placeholder. If you are not sure, start with the Post file, then change it if validation reports a dependency problem.
A realistic file with several types
Suppose you are retiring a legacy service. You delete its Apex class, its test class, the field it populated, and a validation rule that referenced the field. The validation rule must go too, or the field deletion would be blocked:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>LegacyAccountService</members>
<members>LegacyAccountServiceTest</members>
<name>ApexClass</name>
</types>
<types>
<members>Account.RequireOldRegion</members>
<name>ValidationRule</name>
</types>
<types>
<members>Account.OldRegion__c</members>
<name>CustomField</name>
</types>
</Package>Points to notice:
- Two classes share one
typesblock, with onemembersline each. Do not repeat thetypesblock for the same type. - The order of
typesblocks does not control the order of deletion. The Metadata API decides that. - All the components must be removed by the same deployment, or the dependent ones must already be gone. Otherwise the field delete fails because the validation rule still uses it.
The accompanying package.xml
A deployment must include a manifest. If the deployment only deletes, the manifest can have no types, only the version:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>62.0</version>
</Package>Without this file, the Metadata API has no API version and nothing to anchor the deployment. With the CLI, you pass it through --manifest:
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxYou can combine deletions and ordinary changes in one deploy. Put the components you are adding or updating in package.xml, and the ones you are removing in the destructive file. The same component name should not appear in both. A class listed in both would be asked to be saved and removed at once, and the deploy would fail or do something you did not intend.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| `<members>*</members>` in the destructive file | Wildcard is not supported, so the file is rejected | List each component by name |
| `OldRegion__c` instead of `Account.OldRegion__c` | The field is not found | Prefix the object name and a dot |
| Missing `__c` on a custom object or field | The component is not found | Copy the full API name from source |
| Wrong namespace on `Package` | The file fails to parse | Copy the root element from a working manifest |
| Same component in both files | Conflicting actions | List it in only one place |
| Listing a standard field or object | The deployment fails | Leave standard components alone |
| Misspelled type in `name`, such as `ApexClasses` | The type is unknown, so the deployment fails | Use the exact type name from the Metadata API reference |
If you list a component that does not exist in the target org, the Metadata API typically returns a warning that the component was not found rather than an error, and the deployment can still succeed. This is convenient when the same file is applied to several orgs where some have already been cleaned up. It also hides typos, so read the warnings in the deploy output carefully. A "not found" warning for a name you expected to exist is a sign that the name is wrong.
Keeping the files in version control
Treat the destructive changes files as part of your project history. A common layout is:
manifest/
package.xml
destructiveChangesPre.xml
destructiveChangesPost.xmlCommit a destructive file in the same change that removes the source from force-app. A reviewer then sees both halves of the removal together. After the deletion has reached every org you care about, you can empty the file or delete it, so that old deletions do not run again on later deployments.
Exercise
You want to delete the custom field Region__c from Contact, the Apex class OldContactSync, and the report Q3Review stored in the folder SalesReports. Write the types blocks for the destructive changes file.
Show answer
Three types blocks are needed. The class goes under ApexClass with member OldContactSync. The field goes under CustomField with member Contact.Region__c. The report goes under Report with member SalesReports/Q3Review. Each block holds its members line followed by its name line, inside a Package element that has the http://soap.sforce.com/2006/04/metadata namespace. No wildcard is used, and no version element is required.
Exercise
A teammate's destructive file contains <members>*</members> under ApexClass so that "all the old classes" are removed. What is wrong, and what should they do?
Show answer
Wildcards are supported in retrieve and deploy manifests but not in destructive changes files, so the file is rejected. The teammate must list every class to delete by exact name. They can generate the names with sf project generate manifest --source-dir pointed at the classes, then copy the types block into the destructive file.