I would like to know all the reasons for getting the Entity is deleted error. I can't seem to find it on Apex developer docs.

In my case, I'm getting the following error on the after insert trigger of object A, where I delete (only once) records of object B.

System.DmlException: Update failed. First exception on row 0 with id a103N000001APCfQAO; first error: ENTITY_IS_DELETED, entity is deleted: []

The id in the error corresponds with the record I'm deleting. Also, I run this code in the Queueable context when not in Test mode. Test runs the code in the non-queueable or synchronous context, and then I get this error. I don't get it when in Queueable context... if that's relevant.

If anyone has an idea why this error occurs, please help as I've been spending way too much time on this little unit test. Thank you.

EDIT: Here's the code.

ReqsController rc = new ReqsController(reqRequest);
ReqsController.RequirementResponse response = rc.makeRequest();

urs = new Database.UpsertResult[]{};
if (response.radsToInsert.size() > 0) urs.addAll(Database.upsert(response.radsToInsert, Required_Application_Document__c.Upsert_Key__c, true));
if (response.rpsToInsert.size() > 0) urs.addAll(Database.upsert(response.rpsToInsert, Required_Payment__c.Upsert_Key__c, true));
if (response.rfsToInsert.size() > 0) urs.addAll(Database.upsert(response.rfsToInsert, Required_Field__c.Upsert_Key__c, true));
if (response.risToInsert.size() > 0) urs.addAll(Database.upsert(response.risToInsert, Required_Interview__c.Upsert_Key__c, true));
if (response.recsToInsert.size() > 0) urs.addAll(Database.upsert(response.recsToInsert, Required_External_Correspondence__c.Upsert_Key__c, true));

Id[] requiredsToDelete = new Id[]{};
for (SObject required : response.allpreexistingRequireds) {
    Id reqId = (Id)required.get('Id');
    if (!urIdToUr.containsKey(reqId)) {
        System.debug('delete ' + required);
    } else if (urIdToUr.get(reqId).isCreated()) {
        System.debug('inserted ' + required);
    } else {
        System.debug('updated ' + required);

Required_Application_Document__c[] radsToDelete = [SELECT Id FROM Required_Application_Document__c WHERE Id IN :requiredsToDelete AND IsDeleted = false];
Database.delete(radsToDelete, false)

The code that is called on the second line has no DML statements. It only populates the response object, from which I make the upserts.

Nowhere else do I have a DML statement. In fact, I have debug strings set up around all my code and the log shows that all classes ran fully. Then, the original insert statement from the test class errors out.

EDIT: So the error only occurs when the code is run synchronously, in a unit test as well as from the UI. I have no logic set up to see whether a record is deleted or not before doing it. It is only in the Queueable context where the delete is successful. Any ideas why something like this may be taking place?

EDIT: Now I've made a direct query for records that are not deleted and attempt to delete those and I still get the error. Commenting those last two lines out runs the code successfully.

  • 3
    It seems you are referring the deleted Id, somewhere in your code even after that record is deleted, It will be good if your share your code as well – salesforce Developer Aug 26 '20 at 10:32
  • It would be helpful to see the code. The most likely issue is that you are referencing the deleted record again after the delete. There's also a small possibility that some other process in the org also has delete logic, so the record is removed before you get around to deleting it. – JimG Aug 26 '20 at 11:27
  • I've added my code. Thanks guys. After the delete line on the code, nothing else is run or triggered. – parsa_047Fletcher Aug 26 '20 at 11:52
  • Can you try it with allornone parameter in Delete DML as true. – manjit5190 Aug 26 '20 at 20:47
  • yes, it is the same. – parsa_047Fletcher Aug 27 '20 at 8:09

So this particular issue comes as a result of using DMLWrapper from the TDTM framework. Before the delete function, I had added the to-be-deleted records to the DMLWrapper to update. This action takes place at the very end of the transaction, and before the DMLWrapper got to update the records, they were deleted. Thanks everyone for the advice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.