As a Salesforce developer, you know that debug logs are essential for troubleshooting, but they can quickly become a burden. Whether you are hitting storage limits or simply trying to find a specific log in a sea of thousands, clearing the ApexLog table is a common task. However, the standard 'Delete All' button in the Salesforce Setup UI often processes records page-by-page, making it incredibly slow for large volumes.
In this guide, you will learn multiple ways to bypass the UI limitations and delete all your Salesforce debug logs at once using the Tooling API, Salesforce CLI, and other advanced utilities.
1. The Developer Console (Tooling API Method)
The quickest way to delete logs without leaving your browser is through the Developer Console. While a standard SOQL query won't always allow for mass deletion of system objects, the Tooling API provides the access you need.
Follow these steps:
1. Open the Developer Console.
2. Click on the Query Editor tab at the bottom.
3. Check the box labeled Use Tooling API (this is critical).
4. Enter the following query:
sql
SELECT Id FROM ApexLog
5. Once the results load, select the rows you want to remove (or use Shift+Click to select all) and click Delete Row.
This method is native and requires no external tools, but it can still be limited to 2,000 records at a time in the results panel.
2. Mass Deletion via Salesforce CLI (sf and sfdx)
If you are comfortable with the command line, the Salesforce CLI is the most efficient way to handle thousands of logs. By querying the IDs to a CSV and feeding them back into a bulk delete command, you can clear an entire org's log history in seconds.
Using the modern 'sf' syntax:
sf data query -q "SELECT Id FROM ApexLog" -r "csv" > out.csv
sf data delete bulk --sobject ApexLog --file out.csv --wait 1000
Using the legacy 'sfdx' syntax:
sfdx force:data:soql:query -t -q "SELECT Id FROM ApexLog" -r "csv" > out.csv
sfdx force:data:bulk:delete -s ApexLog -f out.csv
Pro Tip: You can refine your query to be more selective. For example, if you only want to delete logs for a specific user or from a specific date, modify your query to: SELECT Id FROM ApexLog WHERE LogUserId = '005...' AND StartTime < 2023-01-01T00:00:00Z.
3. Using Data Loader or Workbench (Bulk API)
When your org has accumulated over 10,000 logs, the Bulk API is the most stable choice. Both Data Loader and Workbench allow you to interact with the ApexLog object.
Using Workbench:
- Navigate to Info > Metadata Types & Entities and ensure
ApexLogis visible. - Go to Data > Query and execute
SELECT Id FROM ApexLog. - Choose Bulk CSV as the view and download the file.
- Go to Data > Delete, upload your CSV, and ensure you check the box Process records asynchronously via Bulk API.
Using Data Loader:
- Open Data Loader and select Export.
- Check Show all Salesforce objects and select ApexLog.
- Export all IDs to a CSV file.
- Use the Delete function in Data Loader and map the ID field from your CSV.
4. Browser Automation and Extensions
Sometimes you just want the UI to do the work for you without manually clicking 'Next' on every page. You can use a JavaScript snippet in your browser's console to automate the 'Delete All' button.
Open your Salesforce Classic debug log page, press F12 to open the Chrome DevTools console, and run this script:
setInterval(
function() {
if (document.querySelector('span[id*="deleteAllStatus.start"]').style.display === 'none') {
document.querySelector('input[value="Delete All"]').click();
}
}
, 250);
This script checks if the deletion process is idle and clicks the button for you every 250 milliseconds until all pages are cleared.
Alternatively, you can use Chrome extensions like Apex Debugger or Salesforce Coding Utility. These tools often include a one-click 'Clear Logs' feature that handles the API calls behind the scenes.
5. Deleting Logs via Apex and REST API
You cannot delete ApexLog records directly using a standard DML statement like delete [SELECT Id FROM ApexLog] in Apex. However, you can use the REST API to perform the deletion. This is useful if you want to build a custom utility within your org.
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/v58.0/sobjects/ApexLog/' + al.Id);
req.setMethod('DELETE');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
HttpResponse res = h.send(req);
System.debug('Status: ' + res.getStatusCode());
}
Note: If you run this from a Lightning Component, your Session ID may not be API-enabled. In that case, you would need to use a Named Credential to handle the authentication.
Best Practices and Common Pitfalls
- Storage Limits: Salesforce has a limit on the total volume of debug logs (usually 1,000 MB). Once reached, Salesforce will prevent you from generating new logs until you clear the old ones.
- Trace Flags: Deleting logs does not stop new ones from being created. Remember to check your Trace Flags in Setup to ensure you aren't logging more data than necessary.
- Tooling API Checkbox: When using the Developer Console, the most common mistake is forgetting to check the 'Use Tooling API' box. Without it, the
ApexLogobject may not appear in your query results. - Performance: For massive deletions (50,000+ logs), always use the Bulk API (Method 3) to avoid timeout issues.
Frequently Asked Questions
Can I delete debug logs using a standard Apex Trigger?
No. ApexLog is a system object and does not support triggers. Deletion must be triggered via the UI, API, or CLI.
Why can't I see the ApexLog object in Data Loader?
You must check the "Show all Salesforce objects" checkbox in the Data Loader export/delete wizard to see system objects like ApexLog.
Does deleting logs affect my org's performance?
Deleting logs themselves doesn't significantly impact performance, but having an excessive number of logs can make the Setup UI and Developer Console sluggish when trying to load log views.
Wrapping Up
Clearing your Salesforce debug logs doesn't have to be a manual, tedious process. For quick tasks, the Developer Console with Tooling API is your best friend. For heavy lifting and automation, the Salesforce CLI is the gold standard. By using these methods, you can keep your org clean and ensure you always have space for the logs that matter most.