function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Peter KayePeter Kaye 

SOQL with API to retrieve deleted records

I am searching for some example SOQL that I can use in a php script that will retrieve all records for a specified object which have been deleted since a specified date and time.  Please can someone point me in the right direction on this.  Thanks.
Best Answer chosen by Peter Kaye
UC InnovationUC Innovation
Hi Peter,

It is possible to query for all records, including deleted records using the ALL ROWS keyword in the query. There should also be a field called isDeleted which you can use to tell which records are deleted.

As for finding the deleted time, you could probably check the last modified date field on the deleted records, since deleted records shouldn't be modified after they are deleted.

Here's some helpful documentation:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_query_all_rows.htm
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_queryall.htm

Hope this helps! Please don't forget to mark best answer if this helped solve your question!

All Answers

UC InnovationUC Innovation
Hi Peter,

It is possible to query for all records, including deleted records using the ALL ROWS keyword in the query. There should also be a field called isDeleted which you can use to tell which records are deleted.

As for finding the deleted time, you could probably check the last modified date field on the deleted records, since deleted records shouldn't be modified after they are deleted.

Here's some helpful documentation:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_query_all_rows.htm
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_queryall.htm

Hope this helps! Please don't forget to mark best answer if this helped solve your question!
This was selected as the best answer
Peter KayePeter Kaye
Thanks UC Innovation.  Two approaches, queryAll  ( instead of query ) and ALL ROWS.  Thanks for the links.  I ended up with queryAll and as you say IsDeleted = 1 (True) gets deleted ( recycle bin )  records.  And for filtering dates you are quite correct, the LastModifiedDate holds the date of deletion, the date when the IsDeleted field getsupdated to true.

Brilliant - thanks for your help here.