Clean Exchange Server 2016 logs (not database)

I’ve noticed that my exchange server started to use more and more of storage. Thankfully I found this this script from ntweekly.com.

It needed a bit of a change as it only cleared “C:\Program Files\Microsoft\Exchange Server\V15\Logging”

here is the modified one which I use. If you are unsure what it deletes, comment the “Remove-Item $FullFileName -ErrorAction SilentlyContinue | out-null” line and run it.

You can add more paths or edit the existing ones.

Set-Executionpolicy RemoteSign$days=0

$IISLogPath=”C:\inetpub\logs\LogFiles\”

$ExchangeLoggingPath=”C:\Program Files\Microsoft\Exchange Server\V15\Logging\”

$ETLLoggingPath=”C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\”

$ETLLoggingPath2=”C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs”

$ETLLoggingPath3=”E:\logs”

Function CleanLogfiles($TargetFolder)

{

write-host -debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder

if (Test-Path $TargetFolder) {

$Now = Get-Date

$LastWrite = $Now.AddDays(-$days)

$Files = Get-ChildItem $TargetFolder -Recurse | Where-Object {$_.Name -like “.log” -or $_.Name -like “.blg” -or $_.Name -like “*.etl”} | where {$_.lastWriteTime -le “$lastwrite”} | Select-Object FullName

foreach ($File in $Files)

{

$FullFileName = $File.FullName

Write-Host “Deleting file $FullFileName” -ForegroundColor “yellow”;

Remove-Item $FullFileName -ErrorAction SilentlyContinue | out-null

}

}

Else {

Write-Host “The folder $TargetFolder doesn’t exist! Check the folder path!” -ForegroundColor “red”

}

}

CleanLogfiles($IISLogPath)

CleanLogfiles($ExchangeLoggingPath)

CleanLogfiles($ETLLoggingPath)

CleanLogfiles($ETLLoggingPath2)

CleanLogfiles($ETLLoggingPath3)

My result are like this as the majority of my logs are on E:\logs

Leave a comment