TechTorch

Location:HOME > Technology > content

Technology

How to Query Recycle Bin Records in Salesforce

May 23, 2025Technology4047
How to Query Recycle Bin Records in Salesforce Salesforce provides a p

How to Query Recycle Bin Records in Salesforce

Salesforce provides a powerful feature to manage deleted records through its Recycle Bin. This feature ensures that important data isn't permanently lost and gives users a chance to recover accidentally deleted records. In this article, we will explore how to query these recycle bin records using Salesforce Object Query Language (SOQL).

Accessing Recycle Bin Records with SOQL

Recycle bin records can be accessed via SOQL, allowing you to query both active and deleted records. To specifically retrieve deleted records, you can include a filter for the IsDeleted field. Here’s a step-by-step guide on how to do this:

Querying Deleted Records

The following example shows how to query deleted records from a custom object called MyCustomObject__c:

SELECT Id, Name, CreatedDate
FROM MyCustomObject__c
WHERE IsDeleted  TRUE
ALL ROWS

This query retrieves the ID, Name, and CreatedDate of all deleted records for MyCustomObject__c.

Accessing Recycle Bin via the API

If you are using the Salesforce API, the same SOQL query structure applies. You can use this query with tools like Workbench, Data Loader, or through Apex code.

Important Notes

Deleted records are retained in the recycle bin for 15 days or until they are permanently deleted. After this period, they cannot be retrieved. Restoring records can be done through the Salesforce UI or programmatically via the API.

Example in Apex

If you want to perform this query in Apex, you can do it like this:

ListMyCustomObject__c deletedRecords  [SELECT Id, Name FROM MyCustomObject__c WHERE IsDeleted  TRUE ALL ROWS];

This code will give you a list of the deleted records that you can further process in your Apex code.

Querying DeleteEvents

Alternatively, you can query directly on the DeleteEvent object to get more detailed information about deleted records. Here’s an example:

SELECT Id, RecordId,  FROM DeleteEvent LIMIT 10

In this query:

Id – Refers to the ID of the DeleteEvent record. RecordId – The ID of the record that was deleted. – The name of the object the deleted record belongs to. DeleteEvent – The API name of the recycle bin object.

Summary

To summarize, querying recycle bin records in Salesforce involves using SOQL with the IsDeleted field and the ALL ROWS keyword. This allows you to retrieve and work with deleted records as needed. Whether you prefer to use SOQL directly or work with the DeleteEvent object, you have the flexibility to suit your specific requirements.