[SalesForce] Delete Operation Too Large – but all related records have been deleted

I had more that 1,00,000 child objects records for a single record of parent Object.
After deleting the child object records when I am deleting the parent record I am getting the below error:-

  • Using Apex Code : Too many records to cascade delete or set null
  • Using Salesforce Interface : Delete Operation Too Large. You can’t delete more than 100,000 combined objects and child records at the same time

I have also deleted the child records from the recycle bin. Still I am getting the same error.

Could anyone please guide me how to resolve this issue.

Best Answer

You must wait between several days up to a week in order to delete the parent record. Even if you empty the recycle bin, the records are not "permanently" deleted until some reaper job on the Salesforce side runs to truly delete them.

If you're in a rush, you can open a case with Salesforce asking to "run a physical delete" on your org. They will require that you agree to empty your recycle bin first.

The deleted records don't count toward your data storage limit.

We have worked around this problem by setting a 'status' field on the parent object to indicate that we want it deleted. After we delete all of the child records, we set the status to 'Deleting'. Then, create a Scheduled Apex job that runs around nightly trying to delete the parent records that are in that status. You must rescue the possible exception and carry on. Sooner or later, the records can be deleted.

Last caveat: sometimes the org gets corrupted and even the 1 week rule doesn't hold true. In that case, you will need to open a case and have them escalate it to R&D for physical deletion. This is rare, but it does happen.

The bit of Scheduled Apex we use looks something like:

List<SupremeQueryResult__c> deletedSnapshots = [SELECT Id FROM SupremeQueryResult__c
                                                WHERE Status__c IN('Deleted','Deleting')];
for(SupremeQueryResult__c result : deletedSnapshots){
    try{
        delete result;
    }
    catch(System.DmlException dmle){
        if(dmle.getDmlType(0) != StatusCode.DELETE_OPERATION_TOO_LARGE){
            throw dmle; //reraise
        }
    }
}
Related Topic