Delete All Debug logs in salesforce org

how to delete debug logs in salesforce with scripts

Let us learn how to delete debug logs in salesforce by using a single npm command.

Step1 : Open package.json file in SFDX Project.

Step 2 : Copy the highlighted lines into Script Block

"scripts": {
    "lint": "eslint **/{aura,lwc}/**",
    "test": "npm run test:unit",
    "test:unit": "sfdx-lwc-jest",
    "test:unit:watch": "sfdx-lwc-jest --watch",
    "test:unit:debug": "sfdx-lwc-jest --debug",
    "test:unit:coverage": "sfdx-lwc-jest --coverage",
    "prettier": "prettier --write \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"",
    "prettier:verify": "prettier --list-different \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"",
    "postinstall": "husky install",
    "precommit": "lint-staged",
    "deleteLogs":"npm run query:debugLogs && (npm run delete:debugLogs || npm run delete:logsFile) && npm run delete:logsFile",
    "query:debugLogs":"sfdx force:data:soql:query -q \"SELECT Id FROM ApexLog\" -r=csv > logs.csv",
    "delete:debugLogs":"sfdx force:data:bulk:delete -s ApexLog -f logs.csv",
    "delete:logsFile":"del logs.csv"
  },
package.json

Step 3 : Open Terminal inside Vs Code

Step 4 : Now the run the below command in Terminal

> npm run deleteLogs
PowerShell

Output :

debug logs in salesforce will be deleted by query and bulk api

Explanation :

  1. query:debugLogs : will query the all the debug logs against the Authorized org and save them in logs.csv
  2. delete:debugLogs : will take all the id from logs.csv file and make bulk api call to delete them
  3. delete:logsFile : will delete the generated logs.csv
  4. deleteLogs : will run query:debugLogs first then delete:debugLogs if its failed then delete:logsFile. If its successfull then delete:logsFile will run.
  5. If you want to debug logs.csv file , remove the command npm run delete:logsFile in deleteLogs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights