This question already has an answer here:

I'm trying to delete the complete Apex debug logs but getting DML operation Delete not allowed on List<ApexLog>.

Code:

List <Apexlog> loglist = [Select Id from Apexlog];
delete loglist;

Please provide an idea to delete complete debug logs in code.

marked as duplicate by glls, Dave Humm, Raul, battery.cord, Mohith Shrivastava Nov 28 at 3:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

You cannot delete the debug logs natively in Apex code. But you can use Rest DELETE endpoint to delete debug logs.

I have created a utility code to delete debug logs that way, you can refer it.

The only limitation is

  1. It can delete only 100 in 1 iteration(You can use composite API to bulkify it)
  2. It creates a new debug log after execution

    List <Apexlog> loglist = [Select Id from Apexlog limit 100];
    for(Apexlog al: loglist){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(Url.getOrgDomainUrl().toExternalForm()
        + '/services/data/v44.0/sobjects/Apexlog/'+al.Id);
        req.setMethod('DELETE');
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
        HttpResponse res = h.send(req);
        System.debug(res.getStatusCode());
    }
    
    System.debug('loglist'+loglist);
    

If you are calling this method from lightning component, your Session ID wont be api enabled. Thus you have to use named credentials .

Src: https://salesforce.stackexchange.com/a/183692/19118

  • 2
    Nice, that is a useful bit of code. – David Reed Nov 16 at 16:05
  • Yeah, its quite handy when your org creates 100'smb of debug log a minute. – Pranay Jaiswal Nov 16 at 16:11
  • 1
    Nice Solution! Note that this works only if you're not in a Lightning context, since Lightning Session IDs would not be API-enabled. – Sebastian Kessel Nov 16 at 16:18
  • Yups, in lightning, one needs to use Named credentials. salesforce.stackexchange.com/a/183692/19118 – Pranay Jaiswal Nov 16 at 16:24
  • May be worth adding this answer to the question flagged as duplicate, as that one seems more canonical given viewership, age, number of answers, etc. – Adrian Larson Nov 30 at 15:30

As far as I can tell, the documentation is mistaken in showing that ApexLog can be deleted from Apex DML.

The object is deletable from the Tooling API, however. You can tackle this in two ways:

  1. You can actually call out to the Tooling API from your Apex code to perform a delete operation on the object. You would make a DELETE method callout to the REST endpoint

    /services/data/v43.0/tooling/sobjects/ApexLog/YOUR_LOG_ID

  2. You can do it directly from the Developer Console, without writing a line of code. Simply write SELECT Id FROM ApexLog in the Query Editor, check "Use Tooling API", and execute the query. Then, select rows in the results display, and click the Delete Rows button to remove them.

  • May be worth adding this answer to the question flagged as duplicate, as that one seems more canonical given viewership, age, number of answers, etc. – Adrian Larson Nov 30 at 15:31

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