How to discover whether a given deleted record is in the recycle bin

recycle-binundelete

Background: I'm adding hard deletes to the Salesforce destination in StreamSets Data Collector. Adding the ability to do hard deletes via the Bulk API is trivial – just specify hardDelete instead of delete as the operation for the job. Where I'm hitting a problem is in testing that a hard delete did, in fact, happen, rather than a soft delete.

As has been discussed in answers to questions such as The netherworld of deleted records not in recycle bin, explicitly hard-deleted records, and records emptied out of the recycle bin, still show up in queries for deleted records alongside the contents of the recycle bin. The final section of the article Recover records and data in Salesforce explains the record deletion deathcycle that causes this behavior – the period of time between records being marked for deletion and actually deleted.

And so we come to the question: is there a way to retrieve the contents of the recycle bin via API (I'm not aware of one!), or a way to determine whether a given record is in the recycle bin or not, short of trying to undelete the record.

I have verified that, as expected, I can undelete a soft-deleted record, and that trying to delete a hard-deleted record located via a SOQL query in Apex does indeed fail:

Account[] a = [SELECT Id, Name FROM Account WHERE Name = 'deleteme' ALL ROWS];

System.debug(a);

undelete a;

Trying the above after hard-deleting the Account 'deleteme' gives:

System.DmlException: Undelete failed. First exception on row 0 with id 0011R00002iWH24QAG; first error: UNDELETE_FAILED, Entity is not in the recycle bin: []

I can try to undelete the record and test for the exceptions, but I'd like a more… elegant approach.

Best Answer

I believe what you're looking for is this query:

Boolean isRecordInRecycleBin = [SELECT COUNT() FROM DeleteEvent WHERE Record = '0011R00002iWH24QAG'] == 1;

Note: This query also returns true for records that are not deleted at all so you'll need to run IsDeleted = TRUE ALL ROWS query first to get all deleted records and then use the ids in the query above to determine if the records are soft or hard deleted

Here's a recycle bin URL in it you can see that the object used is DeleteEvent: https://connect-computing-2597-dev-ed.lightning.force.com/lightning/o/DeleteEvent/list?filterName=00B3F000009ovJvUAI

Reference: https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_deleteevent.htm

Related Topic