Internet Information Services (IIS) is a web server application that is widely used to host and manage websites on Windows servers. IIS generates log files to keep track of various events and activities, including errors, warnings, and other information that can help diagnose and troubleshoot issues on a web server.
Over time, these log files can take up a lot of disk space, and it is essential to clear them periodically to avoid disk space issues. In this blog post, we will explain how to remove IIS log files on a Windows Server.
Table of Contents
Requirements
- A Windows Server with the IIS role installed. This tutorial uses a Windows Server 2022 but should also work on Windows Server versions 2012 through 2019.
- Windows PowerShell 3.0 (minimum, default on Windows 2012).
Find the IIS Log Files Location
Before you can delete the IIS log files, you must first find out their location. Since Windows Server 2008, the default location for the IIS log files has been in %SystemDrive%\inetpub\logs\W3SVC%id where %id is the ID number of the site.
For example, the first website log files are located in %SystemDrive%\inetpub\logs\W3SVC1, while the second website log files are in %SystemDrive%\inetpub\logs\W3SVC2, and so on.
Of course, that’s assuming that you have not customized or moved the IIS log files path. So how do we confirm the IIS log files’ location?
Using the IIS Manager
On the IIS server, press CTRL+R to open the Run dialog, type inetmgr, and press Enter or click OK.
Navigate to SERVER > Sites > Website, and double-click the Logging feature.
The logs path is shown on the Directory box, similar to the image below.
If you have more than one IIS website, repeat the same steps to find the log path for the other websites.
As you can see below, this IIS website has 31 log files with sizes ranging between 100 MB and 150 MB. If left unmanaged, the IIS log files will continue growing, leading to disk space depletion.
Note. Learn how to sync client time with Domain Controller on Windows.
Using PowerShell
Manually checking each website’s IIS log file path in the GUI is fine if you have a small number of sites. But what if you have multiple sites? Clicking through the IIS Manager can take time and effort. So instead, let’s use PowerShell to list each website’s IIS log files location.
# Get IIS Site Log Files Path Import-Module WebAdministration Get-Website | ForEach-Object { New-Object psobject -Property $( [ordered]@{ Site = $_.Name; LogPath = $(($_.LogFile.Directory).ToString().Replace('%SystemDrive%', $env:SystemDrive)) + "\W3SVC$($_.id)\" } ) }
This PowerShell code retrieves a list of websites using the Get-Website cmdlet from the WebAdministration module. For each website, it creates a new PowerShell object with two properties: Site and LogPath.
The Site property contains the website name, while the LogPath property contains the path to the website’s log file directory based on the website’s ID.
You could save this script as Get-SiteLogPath.ps1 and run it like so:
.\Get-SiteLogPath
Delete IIS Log Files
Deleting all IIS log files is not recommended. You should consider retaining the log files from at least the last 3 to 7 days. This way, you still have the log files to review should you need them.
Using the File Explorer
Like any file on the system, you can delete the IIS files from File Explorer. Once you locate the folder, select the files and delete them. You can press Shift+Delete to delete the files permanently without going to the Recycle Bin.
Repeat the same steps if you have more than one website.
Using a PowerShell Script
While deleting files from the File Explorer is okay for one-off housekeeping or a few sites, automating this task using PowerShell is preferable, especially when there are multiple sites and large IIS logs to remove.
Copy the script below and save it as Delete-IISLogs.ps1.
[CmdletBinding()] param ( [Parameter()] [int] $OlderThanXDays = 7 ) # Get IIS Site Log Files Path Import-Module WebAdministration ## Get the IIS logs folder of all websites. $iis_log_folders = @( Get-Website | ForEach-Object { New-Object psobject -Property $( [ordered]@{ Site = $_.Name; LogPath = $(($_.LogFile.Directory).ToString().Replace('%SystemDrive%', $env:SystemDrive)) + "\W3SVC$($_.id)\" } ) } ) ## Delete the IIS log files older than $OlderThanXDays $thresholdDate = (Get-Date).AddDays(-$OlderThanXDays) $iis_log_folders.LogPath | ForEach-Object { Get-ChildItem -Path $_ -Filter *.log | ` Where-Object { $_.LastWriteTime -lt $thresholdDate } | ` Remove-Item -Confirm:$false -Force -Verbose }
This script accepts one parameter called OlderThanXDays, which specifies the starting age of the IIS log file to delete. For example, run the following command to delete the IIS log files older than 7 days.
.\Delete-IISLogs.ps1 -OlderThanXDays 7
You can now confirm that the script deleted the IIS log files older than 7 days.
Create a Scheduled Task to Delete IIS Log Files
Now that you have a working script, let’s create a scheduled task so that it runs unattended at a specific interval.
This code registers a new scheduled task with the following details.
- Task name: Delete IIS Logs Older Than 7 Days.
- Description: Delete IIS Logs Older Than 7 Days.
- Action: Run the Delete-IISLogs.ps1 script with the -OlderThanXDays 7 parameter.
- Trigger: Every Sunday at 1AM.
- Task principal (user): The local SYSTEM account.
- Run level: With the highest privileges.
- Compatibility: The highest version available is always represented by the Win8 value.
# Register the scheduled task $taskParams = @{ TaskName = 'Delete IIS Logs Older Than 7 Days' Action = (New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-File C:\scripts\Delete-IISLogs.ps1 -OlderThanXDays 7') Trigger = (New-ScheduledTaskTrigger -Weekly -At 1AM -DaysOfWeek Sunday) Description = 'Delete IIS Logs Older Than 7 Days' User = 'SYSTEM' RunLevel = 'Highest' Settings = (New-ScheduledTaskSettingsSet -Compatibility Win8) } Register-ScheduledTask @taskParams
Now, open the Task Scheduler app and confirm the new scheduled task.
Conclusion
To sum up, clearing IIS log files is a crucial maintenance task for any Windows Server administrator. The build-up of log files can consume valuable storage space and cause other services to fail.
By following the step-by-step guide discussed in this blog post, you can efficiently clear IIS log files on your Windows Server without compromising the functionality of your website. Regular log file cleaning can keep your server operating smoothly and mitigate disk space-related problems.
Now that you better understand how to clear IIS log files, it’s time to take action and implement these techniques on your server. By doing so, you can enhance server performance and improve overall website performance.
So, give it a try, and share your experience with us in the comments section below!