Sub topic 10
Recycle Bin Behavior and Purge on Delete
This chapter assumes you can write a destructive changes file and run sf project deploy start with a manifest, both covered in earlier chapters. It also assumes you have a sandbox you can break. Here you will learn what happens to a component after a destructive deployment reports success, and how the purge option changes that.
What "deleted" means after a destructive deployment
A successful destructive deployment does not always remove a component for good. For many metadata types, the Metadata API marks the component as deleted and keeps it for a retention period. This works like the Recycle Bin for records, and it has consequences you can run into:
- You can sometimes restore the component and its data.
- The API name can stay reserved, so you cannot create a new component with the same name.
- Soft-deleted components can still count against org limits.
Which of these apply depends on the metadata type. Custom objects and custom fields have the most visible behavior, so most of this chapter focuses on them.
Retention by metadata type
Salesforce keeps soft-deleted custom objects and custom fields for 15 days. After that the platform erases them. During the window you can restore or erase them yourself in Setup.
| Metadata type | After a destructive deployment | Where to find it |
|---|---|---|
| Custom object | Soft-deleted for up to 15 days. Its records are removed with it and come back if you restore the object. | Setup, Deleted Custom Objects |
| Custom field | Soft-deleted for up to 15 days. The field's data is retained while it is soft-deleted. | Object Manager, the object, Fields & Relationships, Deleted Fields |
| Apex class, Apex trigger | Removed. There is no Setup page to undelete them. | Version control is your only recovery path |
| Other types (flows, layouts, and similar) | Behavior varies by type. Do not assume you can restore them. | Check the type's documentation and your source history |
Treat the Recycle Bin as a convenience for fields and objects. It does not replace version control.
Why the soft-deleted state causes problems
Three problems come up in practice.
Name collisions
A soft-deleted custom field or object keeps its API name reserved. Suppose you delete Account.Region__c and then deploy a new Account.Region__c with a different data type. The deployment can fail because the name is still in use by the deleted component. You have two ways out:
- Erase the deleted component in Setup, then deploy again.
- Delete it with the purge option so it never enters the retention window.
Limits
A soft-deleted custom field still counts toward the per-object custom field limit until it is erased. If you delete many fields to make room for new ones and then hit the limit anyway, the deleted fields are the likely cause.
Surprise data retention
A deleted field's data stays in the org until it is erased. If you deleted a field because it held data you must not keep, the delete alone does not meet that requirement. Erase or purge the field.
The purge option
The Metadata API deployment options include purgeOnDelete. When it is true, components listed in the destructive changes files skip the Recycle Bin and become immediately eligible for permanent deletion.
The Salesforce CLI exposes this as --purge-on-delete on sf project deploy start. The flag only applies together with --manifest and a destructive changes file.
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--purge-on-delete \
--target-org my-sandboxThe destructive file is the same format you already know. This one removes a field so that you can recreate it with a new type:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Account.Region__c</members>
<name>CustomField</name>
</types>
</Package>The manifest that accompanies a delete-only deployment can be empty of types:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>62.0</version>
</Package>Run sf project deploy start --help to confirm the flag is available in your CLI version. Flags can change between releases.
Where purge is allowed
Purge on delete is only accepted by sandboxes and Developer Edition orgs. Production orgs reject the deployment. The error says the option can be used only with a sandbox or Developer Edition org. Scratch orgs are based on Developer Edition, so they are normally fine, but test it in yours before you rely on it.
This restriction is deliberate. In production, the Recycle Bin is your last safety net, and Salesforce does not let a deployment remove it.
Purge and dry runs
You can combine --dry-run with the purge flag to validate the deployment without saving anything:
sf project deploy start \
--manifest manifest/package.xml \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--purge-on-delete \
--dry-run \
--target-org my-sandboxA validation does not delete or purge anything. It shows whether the deletion would succeed, for example whether a dependency would block it. Run the validation first, and remove --dry-run only after it passes.
Deleting with `sf project delete source`
sf project delete source deletes the component from the org and from your local project in one step. It sends the deletion to the org as a Metadata API deployment, so the same Recycle Bin rules apply. It has no purge flag. If you need a purged deletion, use a destructive changes file with sf project deploy start --purge-on-delete.
How a deletion flows
Working with the Recycle Bin in Setup
When you deleted without purge and need to clean up or recover, use these steps.
Restoring a deleted field
- Open Setup and go to Object Manager.
- Select the object, then open Fields & Relationships.
- Find the Deleted Fields link. It appears only when the object has soft-deleted fields.
- Choose Undelete on the field.
After restoring, retrieve the field into your project so that source and org match again. The field returns to the org, but your local files and your version control may already show it as deleted.
Erasing a deleted field or object
On the same Deleted Fields page, or in Setup under Deleted Custom Objects, choose Erase. This is the manual equivalent of purge. Erase when you need the API name back or need the data gone now.
Restoring an object
Deleted Custom Objects lists objects in the retention window. Undeleting restores the object, its fields, and its records, as long as nothing has claimed the name in the meantime.
A worked scenario
You need to change Account.Region__c from a text field to a picklist. Salesforce does not let you convert some field types in place, so you plan to delete the field and create it again with the same name. Do this in a sandbox first.
- Retrieve the current field so that it is in version control.
- Write
destructiveChangesPost.xmllistingAccount.Region__c. - Validate with
--dry-run, with and without the purge flag, and read the output. - Deploy the deletion with
--purge-on-delete. - Deploy the new picklist field definition.
Without step 4's purge flag, step 5 would fail while the old field sat in Deleted Fields. You would erase it manually and redeploy.
Then think about production. Purge is rejected there, so the same sequence needs an extra manual step: after the deletion, erase the field in Setup, and then deploy the new definition. Your release notes should list that step. Also note that deleting a field in production discards its data once erased, so decide how to preserve the values first, for example with an export.
Common mistakes
- Assuming deletion is instantly final. A field you deleted is still in Deleted Fields and still counts against limits.
- Using purge in a script that points at production. The deployment fails. Use an alias per environment and check
--target-orgbefore running. - Expecting to restore Apex. Apex classes and triggers have no Recycle Bin. Rely on your repository history.
- Forgetting to retrieve after restoring. Undeleting in Setup does not update your local project.
- Purging before validating. Purge removes your only recovery path inside the org. Always run
--dry-runfirst.
Summary of decisions
- Use the default (no purge) when the org is production, when the data might still be needed, or when you want a 15-day safety window.
- Use
--purge-on-deletein a sandbox when you must recreate a component with the same API name, free up field limits, or make sure deleted data is gone. - Use Setup's Erase to do the same thing manually after the fact, including in production.
Exercise
You deleted the custom field Account.Region__c from a sandbox with a destructive deployment, without the purge flag. A new deployment that creates Account.Region__c with a different type now fails. What are two ways to fix this, and why would neither work by running the same deployment in production with --purge-on-delete?
Show answer
The old field is soft-deleted and still reserves its API name. You can erase it in Setup (Object Manager, Account, Fields & Relationships, Deleted Fields, Erase) and redeploy, or you can redo the deletion with --purge-on-delete in the sandbox so the name is released immediately. In production, --purge-on-delete is rejected because purge works only in sandbox and Developer Edition orgs. There you must erase the field manually in Setup before deploying the new definition.
Exercise
A teammate says that because sf project delete source --metadata ApexClass:LegacyAccountService succeeded, they can restore the class from the Recycle Bin if needed. What is wrong with this, and what should they do instead?
Show answer
Apex classes do not have a Setup restore page like deleted custom fields and objects do, so the Recycle Bin is not a recovery path for them. They should recover the class from version control, for example by checking out the commit that still contains it and deploying it. Also, sf project delete source has no purge flag, so its behavior follows the normal retention rules for each type.