How to: Clear Exchange 2013 / 2016 log files

With Exchange 2013 and especially 2016, Microsoft started logging more than in earlier versions. These logs are intended to help them if you call in with problems. However, it seems like the biggest problem is the space these logs can take up. Once your Exchange server gets below a certain amount of free space on the drive where logs are stored you will see mail queuing up due to back pressure, Event ID 15006.

Some research led me to a couple of solutions; I’m using one with a few tweaks that helped it to run in our environment.

NOTE: these are not the mail logs, please don’t fill the comments telling me that we shouldn’t manually delete any logs from Exchange lest we be visited by plagues of locusts, etc. These are performance and error logs, they can safely be manually deleted and must be, since they are not cleaned out automatically.

7 Steps total

Step 1: Download the PowerShell script

Go to this TechNet article and download the CleanLogs(AccessDeniedFix).PS1 version, do not use the ClearLogs.ps1 download button.

Save it to your normal script file location on the Exchange server. Because we run Software Restriction Policy I store scripts in Program Files but as long as you can run the script then you should be OK.

Step 2: Change Execution Policy

There are a couple of changes to make to the script,

I found that it wasn’t running on all the locations it should and was throwing an error. Based off the error message I changed Line 1 to:

Set-Executionpolicy -Scope CurrentUser RemoteSigned

after which it ran successfully on all the locations included.

Step 3: Set the number of days

Modify Line 2 for the desired number of days to retain logs.

You should plan this along with how often you intend to run the script.

Step 4: Modify for *your* path

Depending on how you set up your Exchange server, you will need to modify the paths to the log files. Make sure Lines 3, 4, 5, 6 have the correct path.

The default script assumes you have Exchange installed on the C drive. If you’ve moved the logs then you need to change the location to suit.

Step 5: Fix Logging Path 2

There is a missing backslash at the end of Line 6, it should read …\Logs\

Step 6: Fix Function

There’s an error in the script on Line 15, where the path is hard-coded, probably while the author was testing.

Replace C:\Program Files\Microsoft\Exchange Server\V15\Logging\ with $TargetFolder

Step 7: Add a Scheduled Task

On the Exchange server create a new Task. Mine is called Log cleanup.

There is a trick to running a PowerShell script, your action is to start a program Powershell.exe and in ‘Add arguments’ you specify the path and name of the script C:\Program Files\CleanLogs(AccessDeniedFix).ps1

As always YMMV, don’t test in Production, take a backup first, YKINMK, etc.

Amazed that Microsoft hasn’t done something about this, since they clearly know about the problem. There are some registry settings you can make to limit the number of log files in certain locations, so maybe that is their solution.

Hope this helps.

Credit due to the people in the Reference section.

Close Menu