You already learned how to bulk convert user mailbox to shared mailbox. This time we…
Cleanup logs Exchange 2013/2016/2019
Starting from Exchange 2013 and higher, logs are taking up more space on the Windows Server. This is when cleanup logs Exchange 2013/2016/2019 script plays an important role. Clear Exchange logs with PowerShell and get free space on the Exchange Server. These logs are NOT database logs! You can safely delete these logs. In fact, I recommend you to delete them.
Logs are great when you need more information and want to have a look into it when you are having issues. Microsoft Exchange Servers are filling up easily with all these logs. The best way is to clear the logs because you need to free up some size on the disk.
Table of contents
Prepare the cleanup logs Exchange script
Copy the following code and paste it in Notepad. Save the file type as CleanupLogs.ps1. You can also download the CleanupLogs.ps1 script (direct link). If it does not give you a prompt to save the file, right-click on CleanupLogs.ps1 and click save link as. You should be able to save the script.
# Set execution policy if not set
$ExecutionPolicy = Get-ExecutionPolicy
if ($ExecutionPolicy -ne "RemoteSigned") {
Set-ExecutionPolicy RemoteSigned -Force
}
# Cleanup logs older than the set of days in numbers
$days = 2
# Path of the logs that you like to cleanup
$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\"
# Clean the 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-Object { $_.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)
Make sure that you change line 8. At the moment, it’s set as $days=2. That means it will cleanup logs from the Exchange Servers older than 2 days.
If you have changed the path of the Exchange configuration, change the path in lines 11, 12, 13, and 14.
Save the file on the Exchange Server in the following path C:\scripts\. Give it the name CleanupLogs.ps1
Give your account permission access to the below four folders. If the script does not have permission, it will not cleanup logs in that folder. I recommend making a service account in Active Directory. Give the service user account read/write permission on the below four folders. Create a scheduled task to clear Exchange logs that will run every day with the service user account.
C:\inetpub\logs\LogFiles\
C:\Program Files\Microsoft\Exchange Server\V15\Logging\
C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\
C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\
Run the cleanup logs Exchange script
Let’s look at how much the script will clear.
Before running the cleanup logs script
Before you run the script, have a look at how much free space you have on the drive. See the before cleanup screen.
Run the script as Administrator. Right-click the file CleanupLogs.ps1 and click on Run with PowerShell. A PowerShell window will show up and the script is cleaning up the logs. Wait till the PowerShell window disappears from the screen. It means that the script finished.
After running the cleanup logs script
Have a look again at how much free space you have on the drive. See the after cleanup screen.
The script did cleanup 14GB of the Exchange logs. Did the cleanup logs Exchange script help you out?
Note: Do you like to automate the script? Read more on how to Cleanup Exchange logs automatically with scheduled task.
Conclusion
You learned how to clear Exchange logs with PowerShell. It’s a great script to cleanup logs on Exchange 2013/2016/2019 and get free space. Don’t forget to create a scheduled task to cleanup logs on the Exchange Server to automate the process.
Did you enjoy this article? You may also like Move Exchange database to another drive. Don’t forget to follow us and share this article.
Quite Helpful article
Very Cool Tool!
I ran it and it works great.
Thank you very much for sharing!
Great script! Removed over 60gb of log files. Only issue we have is it says it is deleting the contents of: ‘C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\’ but it doesn’t. It shows each individual file to be deleted while the script is running. I can manually delete the files without issue. I checked the permission of the folder. SYSTEM & Administrators have full control. I also changed the script to go to the root of Diagnostics but same results. Is there anything else we can do? Only because this folder gets pretty big if left untouched. All the rest worked perfectly. Thanks in advance!
Disregard. When it ran on it’s scheduled time, it deleted everything. Not sure why it didn’t when I ran it manually but it doesn’t matter. Thanks!
3 exchange servers in a DAG. Ran the script on each one. Scripts ran for over 15 mins and recovered over 80 GB each. I can finally stop growing my exchange VM’s C: drive
Thanks
To be honest I was a little apprehensive before running it on one of our servers but it seems to be doing the job great. I cleaned over 400GB and that’s just one exchange server out of 3
Thank you!
Nice Script – but is it possible to change log-level to something lower?
Wow, since I was told to check out your website, this is the 3rd GREAT thing I’ve found most helpful, 30GB freed up. I’ll be checking back regularly for thing I don’t even know I’m missing! THANK YOU
Hola Ali sos de gran ayuda todos tus consejos y permiten aprender mucho, estos Logs que se borrar no se estaría eliminando archivos de Auditoria? o cuales son los archivos que se utilizan para tal fin, saludos desde Argentina.
Great script, freed up 40GB which was desperately needed. Many thanks !
Thx! This is great solution!
Thank you SO freaking much for this. My logging folder accumulates close to 10GBs a day (not including Transaction logs) and was driving me nuts.
Hi Ali,
Can we add in this script to clean up the logs under this folder?
The files without exstension
“D:\ExchangeData\Transport\Queue\TempStorage\UnifiedContent”
Thanks
I recommend following the article Exchange UnifiedContent folder keeps growing.
Thank you so much! This worked great and cleaned up almost 40gb.
Amazing!!! Ran the script and it worked wonders. Thank you!!!!!
Great Script, thank you very much!
Hello
the script will empty only the folders inside the actual folder “C:\Program Files\Microsoft\Exchange Server\V15\Logging\” ?
Thanks
The script will clean up only the logs files (.log/.blg/.etl) from the following directories:
C:\inetpub\logs\LogFiles\
C:\Program Files\Microsoft\Exchange Server\V15\Logging\
C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\
C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\
Thanks alot
Hi
Thank you for the script and it did free up some space
c:\inetpub\logs\logfiles administrator and system have full access.
Under w3svc2 the u_extend1_x.log files is 54GB
Under w3svc1 the u_extend1_x.log file is 24GB
Your script does not touch those files. can I just delete them?
It should remove those .log files.
You can remove them manually. However, if unsure, you can always make a temporary backup before removing it.
Thanks dear, very helpful script. I have another question that is also related to DB logs. In my exchange server 2019, DB logs take too much space, any solution?
The logs you cleaned are NOT database logs (read the article again).
Look at the backup system because it should truncate the database logs, and all the logs will empty.
I recommend going through the following articles:
– Enable circular logging in Exchange Server
– Truncate Exchange logs with PowerShell
100Gb saved on Exchange 2019, worked like a charm.
Thank you very much Ali Tajran for your contribution here, really amazing thing you are doing.
Recently I had to split 1.2TB .edb file to 2 smaller .edb’s, without the guidances in your website I would be lost.
Totally amazing work here!
Thanks, the scripted worked perfect. Gained almost 25GB space 🙂
thanks a lot , this did give me a lot of time and spaces 🙂
Hey Ali! Thank you very much for the script. Why don’t you add C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\ to the script?
You can add that path to the script, but I don’t recommend doing that. That’s because I always recommend enabling logging on the Exchange Server send and receive connectors.
Once you clear the logs in the path you mentioned, you will not see all the send and receive connectors logs history (30 days by default).
Read more:
– Exchange send connector logging
– Exchange receive connector logging
Hi Ali,
Just wanted to thank you for all your great articles and scripts! You’ve saved me many times since I took over our Exchange infrastructure!
Hi Ali,
we got about 90 subfolders under logging. Most used Space is in folder HTTPProxy and MapiHTTP.
Can we also be sure to delete these Logfiles?
Thank you
The script will go through all the “Logging” subfolders and remove the logs.
Seems like a solution!
Bust question: I guess it’s safe to use on any Exchange 2019 Version?
The script is safe and will work on Exchange Server 2013/2016/2019.
Nice work! Exactly the thing I was looking for 🙂 Thanks a lot.
That is great script! It helped me save 100 GB.
After 4 Years Exchange 2016 – 171Gb saved.
Thank you 🙂
Hi, thanks for the great article. I will definitely try it soon 🙂
I just want to ask, do I need to use the script before I use it:
Disable these Exchange services?
– Microsoft Exchange Health Manager Service
– Microsoft Exchange Diagnostics
Thank you
You don’t have to disable any Exchange Server services. Instead, run the script, and you’re good to go.
Cleared 64GB of logs from a customer’s Exchange 2013 server!
Thanks!
Thanks! Works great!
Great article, thanks for providing the script and explanation!
Thank you, works great!
Hi Ali, Great article. Quick question. Are these log files safe to delete on multiple exchange 2013 servers that are part of a DAG without breaking anything?
Thanks
Waseem
Hi Waseem,
These are not database logs and are safe for you to remove. Use my script and when you’re on it, create a scheduled task.
Thank you
Thank you, Mr. Tajran! I just freed up 40GB of logs on an Exchange server that was put in production this past May. I have it set to run every week now.
Why Microsoft doesn’t automatically clear these log files automatically is a mystery to me.
Hi,
Nice article! Thanks!
The script does not clean up c:\inetpub\logs\logfiles
Several services needed to be stopped in order to delete those log files. I did not delete the log files but until stopping services it gave a retry on an attempt to delete the log files. Even after stopping those services and running the script again they did not clean up. Please advise.
Start File Explorer. Go to the C:\inetpub\logs\LogFiles. You will see two folders, which are: “W3SVC1” and “W3SVC2”. Ensure that both the “SYSTEM” and “Administrators” groups have full access permissions (Security tab). After that, try to remove the .log files manually. If that works, the script needs to work too.
Very nice script. Well written and efficient. Thank you!
Hi Ali,
Thanks for the wonderful advice – I just have one question if that’s okay. The Microsoft TechNet article seems to suggest that, within the C:\Program Files\Microsoft\Exchange Server\V15\Logging\ folder, only the contents of the Bin\Search\Ceres\Diagnostics\ETLTraces and Bin\Search\Ceres\Diagnostics\Logs are safe to delete, but your script suggests the entire contents of the C:\Program Files\Microsoft\Exchange Server\V15\Logging\ can safely be removed (obviously where the log files exceed the value in the script file) – can you please confirm which is correct ? Because if your version is accurate, I could save a *tonne* more space (which I desperately need right now) – I just don’t want to run the risk of breaking something as a result.
Thank you so much for your time, I really appreciate it =]
Hi David,
These are not database logs and are safe for you to remove. Use my script and when you’re on it, create a scheduled task.
Thank you so much Ali, you are *the best* =D
This is a great script.
Adjusted the paths as needed and ran it and it works great.
Thank you for sharing!
thanks,
works wonder full.
i am not a full time exchange guru. but this realy helps. ( and cleans the standard MS stuff )
Hi, anyone can help me?
My exchange path is :
$IISLogPath = “C:\inetpub\logs\LogFiles\”
$ExchangeLoggingPath = “D:\Exchange Server\V15\Logging\”
$ETLLoggingPath = “D:\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\”
$ETLLoggingPath2 = “D:\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\”
do i still need to save this script in C:\ ?
You can save the script anywhere on the Exchange Server. I recommend placing it in the scripts folder on the C:\ drive.
Hi,
thank you for the script.
How to clean correctly the Database Logs even if these logs are in a dedicated Drive L:\logs\database_logs_1 ?
My database logs are going full and I wanted to know how to clean it.
Hi George,
In this article, I explained how to cleanup Exchange logs. These are not database logs. Unfortunately, they will not truncate with a backup. That’s why I recommend using the PowerShell script to free up space.
A good backup should truncate the database logs. It can happen that it’s not working at the moment, and you need to free up space. You can follow the article Truncate Exchange logs with PowerShell.
How do we set this up to run it automatically on Server 2016
Have a look at the article Cleanup Exchange logs automatically with scheduled task.
Yeah that worked wonders. Thanks!
Nice script!
It literally cleared 70GB of logs
Never did check those logs, then suddenly the mail queue is hanging. This script was a real life saver, thank you very much!
Thanks a lot, this did save me a lot of space. Right on spot.
Great script which regained me around 20GB of space on my server that was slowly but surely running low on space. I now have time to address my disk space issue at a more leisurely pace. Thanks.