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”.

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.

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&l