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.jsonStep 3 : Open Terminal inside Vs Code
Step 4 : Now the run the below command in Terminal
> npm run deleteLogs
PowerShellOutput :
Explanation :
- query:debugLogs : will query the all the debug logs against the Authorized org and save them in logs.csv
- delete:debugLogs : will take all the id from logs.csv file and make bulk api call to delete them
- delete:logsFile : will delete the generated logs.csv
- deleteLogs : will run query:debugLogs first then delete:debugLogs if its failed then delete:logsFile. If its successfull then delete:logsFile will run.
- If you want to debug logs.csv file , remove the command npm run delete:logsFile in deleteLogs.