# Deleted Record Management

Though CopyStorm will faithfully back up records that have been deleted in Salesforce, there are a few cases where Salesforce will delete records without the deletion being visible to CopyStorm. These situations occur when:

* A *hard delete* is performed in Salesforce.
* A Salesforce table does not track deleted records (this occurs in system tables).

## Best Practices for Hard Deleted Records

When a Salesforce record is deleted, Salesforce marks the record as deleted by setting the “isDeleted” field to true and by putting the record in the recycle bin. When CopyStorm runs it retrieves all updated records — including those which have been marked as deleted. This is used to record record deletion in the CopyStorm database, but there are a few cases where a record is deleted and not put in the recycle bin.

This is called a hard delete. When this happens, the normal CopyStorm update flow will never determine that the record has been deleted.

There are four primary conditions where hard deletes occur:

* A user manually empties a recycle bin.
* A record has been in the recycle bin for a long time (weeks) and Salesforce automatically deletes it from the recycle bin.
* Two records are merged.
* Salesforce deletes a record without using the recycle bin. This does not occur on most tables, but this article identifies the handful of tables with this issue.

To solve this problem, the “isDeleted” field needs to be updated to reflect records that have been hard deleted in Salesforce. The [CopyStorm/Medic fixIsDeleted](/reference/copystorm-medic/fix-isdeleted.md) tool has been designed to do this exact task.

How often (and if) your site resolves hard deletes is up to you. If you would like the CopyStorm backup to reflect hard deletes then the following list may help — it contains known tables that can contain hard deletes in a out-of-the-box Salesforce instance:

| **Table Name**         | **Common Impact** | **Description**                                                                                                                          |
| ---------------------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| OpportunityLineItem    | High              | The operation of syncing an opportunity with a quote does a hard delete on existing line items.                                          |
| OpportunityContactRole | High              | When team assignments change hard deletes from Salesforce can happen.                                                                    |
| .\*History             | Low               | Older history records can be automatically hard deleted by Salesforce.                                                                   |
| .\*Share               | Low               | When sharing rules are modified, Saleforce hard deletes share records.                                                                   |
| .\*Feed                | Low               | Feed records (from Chatter) are sometimes deleted for reasons that are not clear. We suspect this occurs when a chatter feed is deleted. |

Because hard deletes can be caused by a site’s business practices (or by specific user behavior), best practices are often site specific. This is Capstorm’s recommendation:

* If your site uses Salesforce quotes, then run a daily CopyStorm/Medic job to fix hard delete problems within the OpportunityLineItem table.
* Each weekend, run CopyStorm/Medic to fix hard delete problems on all custom tables and core Salesforce tables. Core Salesforce tables are listed in the sample CopyStorm/Medic file found here.

{% file src="/files/wjQhLGhZGuV4Ivk9ffIY" %}

In practice, we have found that very few custom or core Salesforce tables need to be scanned. Over time we reduce the number of tables scanned based on problems reported by CopyStorm/Medic.

## Best Practices for Non-Tracked Records

There are a number of system tables in Salesforce which do not track deletions or allow restoration from the Salesforce recycle bin.

Tables that follow this behavior can be found in the CopyStorm database by running the following SQL query:

* `select distinct tableName from copyForceTableField where tableName not in (select tableName FROM copyForceTableField where name=’IsDeleted’);`

Most of the time a site will not care if these tables contain deleted records. If your site requires that deleted data be pruned, the best practice is to periodically drop or truncate these tables. Once dropped, the next run of CopyStorm will automatically repopulate the tables.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.capstorm.com/best-practices/deleted-record-management.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
