How to Find Deleted Records in Salesforce Using SOQL

In Salesforce, we use SOQL to search an organization’s Salesforce data for specific information. Using the SOQL queries, we can also fetch the deleted records from the recycle bin.

In this Salesforce tutorial, I will explain how to find deleted records in Salesforce using the SOQL query.

Find Deleted Records using SOQL in Salesforce

In Salesforce, whenever we delete a record from any standard or custom object, it does not get directly removed from the Salesforce database. The deleted record will remain in the Salesforce org’s recycle bin for at least 15 days or till the user deletes it manually from the recycle bin.

To query the deleted object records, it is required that the deleted records be in the Salesforce recycle bin. Records that are removed from the recycle bin won’t be fetched and queried.

In the Salesforce SOQL, when we search the object records, we structure the SOQL query in the following way:

Select Id, name from Opportunity where ID = ‘<your ID here>’ and IsDeleted = true

However, this won’t return any records if you run the SOQL query in the query editor. Here, we will add one more parameter, “ALL ROWS,” to get all the rows of the deleted records.

According to that, the SOQL query will be as:

Select Id, name from Opportunity where ID = ‘<your ID here>’ and IsDeleted = true ALL ROWS

But, this SOQL will not be executed, and it will return an “unknown parsing error”.

Query records from recycle bin in Salesforce

We can only execute the ALL ROWS in the Salesforce Apex code. So, to execute the query, to fetch the deleted records, we will execute the query below in the Salesforce Apex anonymous window.

List<Opportunity> deletedOpps = [SELECT Id, Name FROM Opportunity WHERE IsDeleted = true AND Id = '006J30000041mELIAY' ALL ROWS];
System.debug('Deleted Opportunities: ' + deletedOpps);

After entering the above code in the Salesforce anonymous window, click on the execute button. Then, in the next window, select the option debug only.

Now, in the debug window, we can see the details of all the rows of the deleted opportunity records as we defined in the SOQL query conditions.

Use All rows in Salesforce SOQL queries

In the above query, I have parsed the ID of a specific opportunity record to get its details. We can also query the deleted records using the below query, which will return all rows of all deleted records of the opportunity object.

List<Opportunity> deletedOpps = [SELECT Id, Name FROM Opportunity WHERE IsDeleted = TRUE ALL ROWS];
System.debug('Deleted Opportunities: ' + deletedOpps);
How to query deleted records in Salesforce SOQL

This way, we can query the deleted object records in Salesforce. In the above SOQL query, we have used WHERE clause with IsDeleted condition and then added “ALL ROWS“.

If we have used the ALL ROWS without the WHERE and IsDeleted conditions, then it would have returned all the existing and deleted records.

Find Deleted Records using SOQL in Salesforce Workbench

Another way we can query the deleted object records in Salesforce is by using the Salesforce Workbench. To open the Salesforce Workbench, follow the url “workbench.developerforce.com”, log in with your credentials, and follow the steps below.

1. After login, in the next window, select the “Jump to” field as SOQL query, then in the Object field select the object for which you want to query the deleted records.

After this, click on the Select button.

Use Workbench to query deleted Salesforce records

2. In this step, we have to select the objects, fields, and criteria to build an SOQL query.

Activate the radio button Include in Delete and archived records. Select the object fields to display from the Fields, and in the Filter results by, select “IsDeleted =True” to get deleted records.

Unlike the apex code, the “ALL ROWS” is an invalid parameter in SOQL.

After selecting the fields and criteria, click on the Query button.

Query deleted records from Salesforce Workbench

3. After executing the SOQL query through the workbench, we can see the queried deleted records of the opportunity object.

Use Salesforce workbench to query deleted records

This way, we can query the deleted records of an object using the Workbench in Salesforce.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.