Deprecation of components using Salesforce CLI is achieved through destructive changes manifests, which allow to safely remove metadata from the org.
Salesforce CLI supports a structured and safe approach to metadata cleanup using pre- and post-destructive change manifests. This allows teams to:
By integrating these manifests into your deployment workflow, you can safely deprecate obsolete metadata while maintaining org integrity and deployment consistency.
A destructiveChanges.xml file defines metadata components you want to delete from your Salesforce org. It’s used in deployment workflows to clean up obsolete elements either before or after deploying new metadata.
Sample destructivechanges.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>MyOldComponent</members>
<name>ApexClass</name>
</types>
<version>63.0</version>
</Package>Use Specific Names: List exact component names to avoid accidental deletions.
Group by Type: Organize deletions by metadata type (e.g., ApexClass, CustomObject, Workflow)
Creating a Minimal package.xml for Destructive Changes
Even though you're only deleting components, Salesforce mandates the presence of a package.xml file in the deployment directory. This file can be completely empty except for the version declaration.
Minimal example:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>63.0</version>
</Package>Why It's Required
Deprecated components were successfully validated and removed using Salesforce CLI, leveraging both pre- and post-destructive change manifests. This structured approach ensured:
Deletes components before deploying new metadata..
Purpose: Removes components listed in destructiveChangesPre.xml before deploying new metadata. This ensures that outdated, conflicting, or deprecated components do not interfere with the deployment process.
Use case:
sf project deploy start --manifest manifest/package.xml --pre-destructive-changes manifest/destructiveChangesPre.xml --target-org aliasNamePurpose: Deletes components listed in destructiveChangesPost.xml after the deployment completes.
Use case: Clean up legacy components that are no longer needed once the new metadata is in place.
sf project deploy start --manifest manifest/package.xml --post-destructive-changes manifest/destructiveChangesPost.xml --target-org aliasNameWhat Each Flag Does:
Purpose: Running a deployment validation with pre- and post-destructive change manifests helps ensure that deprecated metadata can be safely removed either before or after deploying new components without compromising org integrity.
Why Validate?
Pre-Destructive Validation
sf project deploy validate --manifest manifest/package.xml --pre-destructive-changes manifest/destructiveChangesPre.xml --target-org aliasNamePost-Destructive Validation
sf project deploy validate --manifest manifest/package.xml --post-destructive-changes manifest/destructiveChangesPost.xml --target-org aliasName
Clean up unused metadata regularly to keep your org lean.