Sub topic 3
Deleting Source Components with the CLI
Assume you have a Salesforce DX project, sf installed, and authorization to a sandbox. You should also know the metadata type and exact member name you want to remove.
Delete a Source Component
Use sf project delete source when you want to delete a named component from the org and remove its local source representation:
sf project delete source \
--metadata ApexClass:LegacyAccountService \
--target-org my-sandboxThe value before the colon is the metadata type. The value after it is the component name. You can also specify a source directory when the component is tracked in your project.
This command is convenient for one-off deletions. For several coordinated deletions, use a destructive changes file with a deployment manifest. That approach gives you a reviewable list and fits better into version control.
Recycle Bin and Permanent Deletion
Deleting metadata is different from deleting records. Record deletion can place records in the Salesforce Recycle Bin. Metadata deletion follows the rules of the metadata type and deployment operation. Some deleted components can be recovered or remain temporarily available, while others cannot.
A Metadata API deployment can request permanent deletion with the purgeOnDelete option. Treat this as irreversible and confirm that the component is no longer needed. Purging metadata does not mean that dependent records or unrelated components are automatically removed.
Why Deletions Fail
Common causes include:
- The member name is wrong. A custom field uses
Object.Field__c, such asAccount.Region__c. - Another component still depends on the item. For example, a page layout, flow, or Apex class may reference a field.
- The component is part of a package or managed dependency that controls its deletion.
- The target org does not contain the version represented by your local source.
- The deletion occurs in the wrong order. Delete references before deleting the referenced component.
Read the deployment errors, correct the names or dependencies, and rerun validation. Do not remove unrelated metadata just to make the deployment pass.
A Safe Deletion Workflow
- Copy the deletion list into version control.
- Confirm each metadata name against the target org.
- Search the project for references and remove or update them.
- Deploy the destructive changes to a sandbox with
--dry-run. - Review the results with another person.
- Run the approved deployment against production.
- Record the deployment result and any recovery action.
Exercise
When should you prefer a destructive changes deployment over sf project delete source?
Show answer
Prefer it when deleting multiple related components or when you need a reviewable, version-controlled deployment.