5

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.

0

2 Answers 2

21

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

6
  • 2
    Nice, that is a useful bit of code.
    – David Reed
    Nov 16, 2018 at 16:05
  • Yeah, its quite handy when your org creates 100'smb of debug log a minute. Nov 16, 2018 at 16:11
  • 2
    Nice Solution! Note that this works only if you're not in a Lightning context, since Lightning Session IDs would not be API-enabled. Nov 16, 2018 at 16:18
  • Yups, in lightning, one needs to use Named credentials. salesforce.stackexchange.com/a/183692/19118 Nov 16, 2018 at 16:24
  • 1
    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, 2018 at 15:30
4

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.

2
  • 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, 2018 at 15:31
  • 1
    We don't actually have to check "Use Tooling API" checkbox to be able to delete the ApexLog record from the Query Editor tab. Jun 25, 2020 at 14:11

Not the answer you're looking for? Browse other questions tagged .