+ Start a Discussion
Francisco Riccomagno 1Francisco Riccomagno 1 

How to delete ApexTestResult records

Hello, I have an org where we have more than 14000 test methods and we ran the whole suite of tests daily. As you can imagine the ApexTestResult grows very fast. I have been going and manually deleting those records through the UI in a weekly basis, but would like to know if there is a way to create a schedule job that does this. I tried to delete this through the exceute anonymous by doing
List<ApexTestResult> apexTestResultList = [SELECT Id FROM ApexTestResult];
system.debug('apexTestResultList found: ' + apexTestResultList.size());
delete apexTestResultList;
But I get an error that says "Delete not allowed on List<ApexTestResult>"
Is there a way to achieve this programatically?
Thank you.

 
Raj VakatiRaj Vakati
Option 1 : You can do it by using rest API 

https://developer.salesforce.com/docs/api-explorer/sobject/ApexTestResult/delete-apextestresult-id
/services/data/v39.0/sobjects/ApexTestResult/{Id}

Option 2 : Setup --> Apex Test Execution-->  View Test History -->Clcik on Clear test data button 
Francisco Riccomagno 1Francisco Riccomagno 1
Thanks Raj, but I'm asking on a way to do this programatically (within Apex)
Raj VakatiRaj Vakati
No .. Because ApexTestResult is not supported delete DMLOperation.