4

In my developer sandbox the Apex logs count has exceeded beyond its limit and there are more than 1 lakh records created in it. I want to mass delete them, but not able to do so.

I am using the windows machine, and tried running the below commands to achieve the same.

sfdx force:data:soql:query -q "SELECT Id FROM ApexLog" -r "csv" | out-file -encoding utf8 out.csv
sfdx force:data:bulk:delete -s ApexLog -f out.csv

But I am receiving an error in the console saying

ERROR running force:data:bulk:delete:  InvalidBatch : Records not found

Can anybody please let me know what am I doing wrong, and whether this can be achieved by any other ways.

7
  • I run the query you have listed via the Salesforce Developer Console's "Query Editor" on the org, click the first entry in the results, scroll to the bottom, shift-click on the last entry (to select all entries) and click the "Delete Row" button then wait while the entries are slowly deleted (takes longer for more entries).
    – Phil W
    Commented Aug 14, 2020 at 14:14
  • Hey Phil, I tried doing the same too. But what I noticed is, even though it shows me as 2000 rows selected, when I hit the delete it deletes not more than 100 records at a time. Also it takes a lot of time to delete around 1.5 lakhs of records. So was trying to find a work around for the same. Commented Aug 14, 2020 at 14:24
  • 1
    No argument there - it is clunky and slow. We'll see if someone responds with a better way!
    – Phil W
    Commented Aug 14, 2020 at 14:27
  • Is csv file empty? One other thing I would do is try using the csv file using dataloader and see if it works? This most likely seems like a encoding issue on windows. I use a similar command on MacOS and it works.
    – javanoob
    Commented Aug 14, 2020 at 15:23
  • @javanoob The csv file isn't empty. Do the dataloader supports ApexLogs object? As the windows has UTF-8 formatting, i have changed the command accordingly. But still no luck. Commented Aug 14, 2020 at 15:39

6 Answers 6

7

The following worked for me

sfdx force:data:soql:query -q "SELECT Id FROM ApexLog" -r "csv" | out-file -encoding oem out.csv
sfdx force:data:bulk:delete -s ApexLog -f out.csv

With other encoding like utf8,utf16 etc SFDX was uploading a file with header only no -values,so it was giving error running force:data:bulk:delete: InvalidBatch : Records not found

1
  • And that worked. Thank you! @Pmanglani Commented Aug 16, 2020 at 15:19
8

The following worked fine for me in MAC

sfdx force:data:soql:query -q "SELECT Id FROM ApexLog" --resultformat csv > out.csv

sfdx force:data:bulk:delete -s ApexLog -f out.csv

I have not tried on windows but the commands above should work fine.

1
  • Thank you for the answer Mohith, but In windows it's not working. May be because of the format issue. It's still throwing the same error. Commented Aug 15, 2020 at 2:45
1

The force:data commands are now deprecated - although not gone yet. A more up-to-date version is:

sfdx data query --query "SELECT Id FROM ApexLog " -r "csv" > ApexLogsToDelete.csv
sfdx data delete bulk --sobject ApexLog --file ApexLogsToDelete.csv
sfdx data delete resume --use-most-recent

The 'resume' command is just there so you can monitor your job. --use-most-recent is a shortcust though, to using the actual job Id - if another job starts, you'll get the wrong batch. But, when you run the data delete bulk command, it does tell you the command to view the actual batch in SF

0

You can use sfdx-hardis command to delete all logs of an org

sfdx hardis:org:purge:apexlog

If you are not connected to an org, it will prompt you for one

This command is also available in VsCode SFDX Hardis menu Debugger

enter image description here

0

enter image description here

The following worked fine for me in Window VS Code. Please run these two commands:

 1. sfdx force:data:soql:query -q "SELECT Id FROM ApexLog" -r "csv" | out-file -encoding oem out.csv`

 2. sfdx force:data:bulk:delete -s ApexLog -f out.csv
0

You can also try this one: https://github.com/osieckiAdam/osiecki-sfdx-plugins. This is a small plugin created by me, but it does its job well.

To run a delete job and track the progress, use the following command:

sfdx oa:apex:log:delete

To run a delete job asynchronously and get the SFDX command to track the progress, use the -a flag:

sfdx oa:apex:log:delete -a

If you want to delete only your debug logs, use the -m flag:

sfdx oa:apex:log:delete -m

To delete debug logs created by any user/users, use the -c flag followed by the user names of the users:

oa:apex:log:delete -c -n [email protected],[email protected]

To delete debug logs with specified statuses, use the -s flag followed by the statuses in double quotes:

sfdx oa:apex:log:delete -s "Internal Salesforce.com Error,success"

You can install this plugin using the standard install command for plugins:

sfdx plugins:install osiecki-sfdx-plugins

And rest assured, it works on Windows, Mac, and Linux.

You must log in to answer this question.

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