IIS (Internet Information Services) Web Server on Windows Server generates a sufficiently large amount of log files during its work. The main problem is that by default IIS log files located on the system drive, which usually has a small size. Insufficient space on the system drive may eventually cause to server crash. Continuous extension of the system drive (even on a virtual machine) due to the large size of the IIS log files also is not the best solution.
Delete IIS Log Files on Windows Server 2012
In this article we will show you how to handle the IIS log files and automate the process of their deleting on Windows Server 2012.
By default, IIS logs stored in the directory:
- Windows Server 2003: %windir%\system32\LogFiles
- Windows Server 2012/2008: %SystemDrive%\inetpub\logs\LogFiles
Administrator can change the path to the logs using the Internet Information Services (IIS) Manager snap-in by specifying the required directory in the Logging section of site settings.
In our case, on Exchange Server 2013 with almost 1,000, IIS generate about 200 MB file per day. Thus, for the year it accumulated about 70 GB of logs!
In case of exhaustion space on the servers system drive, system administrators are beginning to look for what takes so much space, and safely passed inetpub folder, because, at first glance, it has small size. This is because by default Administrator does not have permissions on the subfolders of inetpub, so File Explorer does not show the real size of folder.
Try to open each subfolder in the directory %SystemDrive%\inetpub\logs\LogFiles, agreeing with the appointment of the necessary permission and eventually see that the real volume of folders is not so small.
As a rule, you can safely remove all the log files older than 3-7 days. You can do this manually (not the best choice), or automatically using PowerShell script that will be cleaning log files on schedule.
PowerShell script that will remove all files older than 7 days in the directory C:\inetpub\logs with the *.log extension might be look like this:
gci ‘C:\inetpub\logs -Include ‘*.log’ -Recurse | ? LastWriteTime -LT (Get-Date).AddDays(-7) | Remove-Item
To run this script automatically, create a new job using Task Scheduler:
- Run Task Scheduler
- In the Action pane select Create Basic Task
- Specify task Name: CleanUpIISLogs
- Trigger task to start weekly on Sunday
- Action: Start a program
- Program: exe
- Arguments: -NoProfile -command “gci ‘C:\inetpub\logs’ -Include ‘*.log’ -Recurse | ? LastWriteTime -LT (Get-Date).AddDays(-7) | Remove-Item”
- Open Properties of the created task
- Now change the user under which the task runs to NT AUTHORITY\System and check the option Run with highest privileges
- To test the task, right click on it and select Run
- Make sure that the log files older than 7 days is removed
Bonus tip. Enabling NTFS compression for the whole folder is another great way to quickly reduce the size of the directory with IIS log file. Since log files contain simple text records, their size is reduced significantly when compressed (approximately in 4x times). To enable compression, select Logs folder properties and click on the Advanced button. Check the box Compress contents to save disk space and press OK twice.