Apex – DML Operation Delete Not Allowed: How to Fix

I am receiving the following errors:

DML operation Delete not allowed on List<AccountHistory>
DML operation Delete not allowed on List<ContactHistory>

List<ContactHistory> conHis = [SELECT id, ContactId FROM ContactHistory];
delete conHis; 

List<CaseHistory> caseHis = [SELECT id, CaseId FROM CaseHistory];
delete caseHis; 

Best Answer

You can't ordinarily delete field history before you Enable delete of Field History and Field History Archive. Therefore, if you try to deploy this code to an org without this feature enabled, you'd get this error. The change set is presumably correct. This is generally true for all kinds of optional features in Salesforce. You can't deploy code that would violate the configuration settings for the org (e.g. trying to modify share records with Public Read/Write enabled). Enable the feature and try the deployment again.

Related Topic