[SalesForce] Permanently delete records using apex

Trying to delete records from recyclebin using following code:

List<AsyncLogger__c> deleteList = [SELECT id from AsyncLogger__c WHERE isdeleted = true limit 200 all rows];
List<Id> iDL = new List<Id>();
for(AsyncLogger__c a : deleteList){
    iDL.add(a.id);
}
system.debug(iDL.size());
system.debug(iDL[0]);
Database.emptyRecycleBin(iDL);

I initially tried Database.emptyRecycleBin(deleteList) but after seeing that list<ids> has to be passed, i am getting list of ids and using that.

But records still exist in recyclebin. Is there anything I have to add ?

Scenario: There is a scheduler which checks all cases for closed status and deletes them. But occasionally recycle bin is getting full. So, is there is an option to directly delete record permanently instead of deleting to recycle bin and deleting from there ?(second part, deleting from recycle bin is also not working for me)

Thanks

Best Answer

The Recycle Bin link in the sidebar lets you view and restore recently deleted records for 15 days before they are permanently deleted. Your Recycle Bin record limit is 25 times the Megabytes (MBs) in your storage. For example, if your organization has 1 GB of storage then your limit is 25 times 1000 MB or 25,000 records. If your organization reaches its Recycle Bin limit, Salesforce automatically removes the oldest records if they have been in the Recycle Bin for at least two hours.

Your problem arises because the records have not been hard deleted from the recycle bin. Salesforce runs a process on a scheduled basis that physically deletes records that have been deleted from the recycle bin, if that process has not run yet, you will still see the records there. A fuller description of the behavior of emptyRecycleBin can be found here: Whats the Actual meaning of ALL ROWS in SOQL?