During its operation, the IIS (Internet Information Services) Web Server on Windows Server generates a huge number of log files. The biggest issue is that IIS log files are stored on the system drive by default, which is usually minimal. Inadequate space on the system drive may cause the server to crash. Because of the high size of the IIS log files, extending the system drive (even on a virtual machine) 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 scenario, IIS generates roughly 200 MB of files each day on Exchange Server 2013 with about 1,000 users. As a result, it accumulated almost 70 GB of logs over the course of the year!
When the system drive on a server runs out of space, system administrators start looking for what’s taking up so much space, and they safely skip the inetpub folder because it appears to be little. Because Administrator does not have permissions on inetpub’s subfolders by default, File Explorer does not display the folder’s true size.
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 general rule, log files older than 3-7 days can be securely deleted. You can do this manually (which isn’t the greatest option) or automatically with a PowerShell script that cleans log files on a regular basis.
A PowerShell script to remove any files with the *.log extension older than 7 days from the directory C:inetpublogs would 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”
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
Ensure that any log files older than 7 days have been deleted.
Bonus point. Another wonderful technique to quickly reduce the size of the directory with IIS log file is to enable NTFS compression for the entire folder. Because log files include only simple text data, compressing them reduces their size greatly (approximately in 4x times). Select Logs folder properties and click the Advanced button to enable compression. To preserve disc space, check the box Compress contents and press OK twice.