Sub topic 10
Recycle Bin Behavior and Purge on Delete
This chapter assumes you can write a destructive changes file and run a deployment that deletes components, as covered earlier in this tutorial. It explains what happens to a component after the deletion succeeds, and how the --purge-on-delete flag changes that.
Deleted does not always mean gone
When a destructive deployment succeeds, the component disappears from the org's active metadata. For some metadata types, Salesforce keeps the deleted component for a while so an administrator can restore it. This is a soft delete.
Two cases matter most in practice:
- Custom fields and custom objects are soft deleted. A deleted custom field stays in a deleted state for 15 days. You can restore it from Setup, under the object's Deleted Fields list, or in Deleted Objects for custom objects. After the retention period, Salesforce removes it permanently.
- Many code and configuration types, such as Apex classes, are removed immediately. You can't restore them from a Recycle Bin. Your only copy is in version control.
Retention behavior varies by type. Check Salesforce's documentation for any type you rely on before assuming it can be restored.
Why soft deletes cause trouble
A soft-deleted field still occupies its API name. If you delete Account.OldRegion__c and then try to deploy a new field with the same name, the deployment can fail because the deleted field still holds the name. The deleted field can also still count toward the object's custom field limit.
This is a common problem in sandboxes. You delete a field, redeploy a changed version, and hit a name conflict.
Purging on delete
The deployment option purgeOnDelete tells Salesforce not to keep deleted components in the Recycle Bin. The components become immediately eligible for permanent deletion. In the CLI, the option is the --purge-on-delete flag on sf project deploy start.
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--purge-on-delete \
--target-org my-sandboxSalesforce allows this option only in sandbox, scratch, and Developer Edition orgs. A deployment to a production org with this option set is rejected. Production deletions always follow the normal retention rules.
The flag affects only components listed in the destructive changes files. It doesn't change how deployed components are saved.
Choosing whether to purge
| Situation | Purge on delete? | Reason |
|---|---|---|
| Sandbox cleanup, you plan to recreate a field with the same name | Yes | Frees the API name immediately |
| Sandbox deletion you might want to undo | No | Keeps the restore option |
| Production deletion | Not available | Salesforce rejects the option in production |
| Scratch org in a CI pipeline | Yes | The org is disposable and repeatable runs need clean names |
Practical checks
- After a deletion, open Setup in the sandbox and look at Deleted Fields or Deleted Objects to confirm what was kept.
- If a later deployment fails with a name conflict, check for a soft-deleted component with that name. Either wait for the retention period, erase it from the deleted list in Setup, or redeploy with a different name.
- Test the same destructive files with
--dry-runfirst, then run them in the sandbox with the flag you plan to use. Production behavior won't include purge, so a sandbox run with--purge-on-deletecan hide a name conflict you will meet in production.
Exercise
You delete the custom field Account.OldRegion_c from a sandbox with a destructive changes file. An hour later, a deployment that creates a new field named Account.OldRegion_c fails. What is the likely cause, and what are two ways to resolve it? Would --purge-on-delete have helped in production?
Show answer
The deleted field is soft deleted and still holds its API name for the retention period (15 days). To resolve it, erase the field from the Deleted Fields list in Setup, or redeploy the new field under a different name. Running the original deletion with --purge-on-delete would have avoided the conflict in the sandbox. It would not help in production, because Salesforce does not allow purgeOnDelete there, so production deletions keep the normal retention behavior.