Over time, 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.
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\LogFiles\W3SVC<ID> where %id is the ID number of the site.
For example, the first website log files are located in %SystemDrive%\inetpub\logs\LogFiles\W3SVC1, while the second website log files are in %SystemDrive%\inetpub\logs\LogFiles\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.

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.
Delete the IIS files 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.

Delete the IIS files 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,
[Parameter()]
[switch]
$WhatIf
)
# Get IIS Site Log Files Path
Import-Module WebAdministration
## Get the IIS logs folder of all websites.
$iis_log_folders = @(
Get-Website | ForEach-Object {
[PSCustomObject]@{
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)
# Store deleted file information
$DeletedFiles = @()
# Create log folder if it doesn't exist
$LogFolder = "C:\Logs"
if (!(Test-Path $LogFolder)) {
New-Item `
-Path $LogFolder `
-ItemType Directory `
-Force | Out-Null
}
$iis_log_folders.LogPath | ForEach-Object {
if (Test-Path $_) {
Get-ChildItem -Path $_ -Filter *.log |
Where-Object { $_.LastWriteTime -lt $thresholdDate } |
ForEach-Object {
try {
Remove-Item `
-Path $_.FullName `
-Confirm:$false `
-Force `
-ErrorAction Stop `
-Verbose `
-WhatIf:$WhatIf
$DeletedFiles += [PSCustomObject]@{
FileName = $_.Name
FullPath = $_.FullName
SizeMB = [math]::Round($_.Length / 1MB, 2)
Action = if ($WhatIf) { "Preview" } else { "Deleted" }
DeletedOn = Get-Date
}
}
catch {
$DeletedFiles += [PSCustomObject]@{
FileName = $_.Name
FullPath = $_.FullName
SizeMB = [math]::Round($_.Length / 1MB, 2)
Action = "Failed"
DeletedOn = Get-Date
}
Write-Warning "Failed to delete file: $($_.FullName)"
}
}
}
else {
Write-Warning "Log folder not found: $_"
}
}
# Export deletion log
$DeletedFiles |
Export-Csv `
-Path "C:\Logs\IISLogCleanup.csv" `
-NoTypeInformation
# Display summary
$TotalFiles = $DeletedFiles.Count
$TotalSizeMB = [math]::Round(
($DeletedFiles | Measure-Object SizeMB -Sum).Sum,
2
)
Write-Host "Processed files: $TotalFiles"
Write-Host "Total size: $TotalSizeMB MB"
Write-Host "Log saved to C:\Logs\IISLogCleanup.csv"
Note that when the -WhatIf switch is used, no files are actually removed. The generated CSV log marks these entries as Preview so you can review which files would be deleted before running the script for real.
The script mentioned above checks whether each IIS log directory exists before attempting to delete files. This helps prevent errors if a website has been removed/its log directory is no longer available.
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

In order to preview which IIS log files would be deleted without actually removing them, run the following command:
.\Delete-IISLogs.ps1 `
-OlderThanXDays 7 `
-WhatIf
You can now confirm that the script deleted the IIS log files older than 7 days.
The script mentioned above also records info about deleted files and exports the results to a CSV file. You can use this log for auditing purposes (it includes the file name, full path, size, and deletion timestamp).

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 '-ExecutionPolicy Bypass -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
If you use PowerShell 7, you need to replace powershell.exe with pwsh.exe in the scheduled task action.

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!
Can I use PowerShell to view IIS log file locations for all websites?
Yes. The WebAdministration module provides access to IIS site configuration. You can use the Get-Website cmdlet to retrieve all websites and generate a list of their corresponding log file directories, which is especially useful when managing multiple IIS sites.
Is it safe to delete IIS log files?
Yes, but it is generally recommended to retain recent logs for troubleshooting and auditing purposes. Many administrators keep the last 3โ7 days of logs and remove older files to prevent excessive disk space consumption.
Can I schedule IIS log cleanup to run automatically?
Yes. You can create a scheduled task using PowerShell or Task Scheduler to run the cleanup script on a recurring basis, such as every Sunday at 1:00 AM. Running the task under the SYSTEM account with elevated privileges ensures it can access and remove IIS log files without user intervention.
What happens if an IIS website has been removed but its log folder no longer exists?
The cleanup script checks whether each log directory exists before attempting deletion. If a log folder is missing, the script skips it and records a warning instead of generating an error, making it safer to run in environments where websites are frequently added or removed.
Why should IIS log files be cleaned up regularly?
IIS logs can grow rapidly on busy web servers and consume significant disk space over time. Regular cleanup helps:
- Prevent disk space exhaustion
- Maintain server stability
- Reduce storage usage
- Simplify log management
- Improve operational maintenance processes
Automating log retention with PowerShell and Scheduled Tasks is a common best practice for IIS servers.
