How To Automate IIS Log Cleanup



Last updated: October 28th, 2026

Updated October 2026: the cleanup script has been rewritten to be simpler and safer (it only deletes .log files in each site's own folder), and the post now covers where IIS logs are stored, HTTPERR logs, and a one-command scheduled task.

Problem

A user messaged me that the photo they tried uploading wasn't going through. I couldn't recreate the issue, until another user reported the same thing. When I logged onto the server, the disk was full. File uploads were written to a temporary location before going to a CDN, and there was no room left. The culprit was IIS logs: they had quietly consumed all of the disk space. Deleting them fixed it, but that was a manual job, and it would happen again.

IIS writes a line for every request and never deletes old log files on its own. On a busy server, or one with many sites, that adds up to gigabytes. I needed a cleanup that runs every day, covers every site on the server, and doesn't need editing when sites are added or removed.

Where Are IIS Logs Stored?

  • Site logs: %SystemDrive%\inetpub\logs\LogFiles\W3SVC<site id>\, for example C:\inetpub\logs\LogFiles\W3SVC2\u_ex261029.log. The number is the site ID shown in IIS Manager.
  • HTTP.sys error logs: C:\Windows\System32\LogFiles\HTTPERR\.
  • A site can use a different folder: check IIS Manager > site > Logging > Directory.

Is It Safe To Delete IIS Log Files?

Yes. IIS log files are only records of past requests; IIS doesn't need them to run. The file currently being written may be locked, but older files can be deleted any time. Keep as many days as you need for troubleshooting, analytics or compliance (30 days is a common choice), and copy them elsewhere first if you must keep them longer.

Solution: A PowerShell Cleanup Script

Save this as C:\scripts\CleanIisLogs.ps1. It reads each site's configured log folder, so new sites are picked up automatically:

# C:\scripts\CleanIisLogs.ps1 - delete IIS log files older than N days for every site
# Run elevated in Windows PowerShell 5.1 (WebAdministration module).
Import-Module WebAdministration

$daysToKeep = 30
$cutoff     = (Get-Date).AddDays(-$daysToKeep)

foreach ($site in Get-Website) {
    # Site log folders look like %SystemDrive%\inetpub\logs\LogFiles; each site writes to W3SVC<id>.
    $baseFolder = [Environment]::ExpandEnvironmentVariables($site.logFile.directory)
    $siteFolder = Join-Path $baseFolder "W3SVC$($site.id)"

    if (-not (Test-Path $siteFolder)) { continue }

    $oldFiles = Get-ChildItem -Path $siteFolder -Filter *.log -File |
        Where-Object { $_.LastWriteTime -lt $cutoff }

    foreach ($file in $oldFiles) {
        Write-Output "Deleting $($file.FullName)"
        Remove-Item -Path $file.FullName -Force
    }
}

# HTTP.sys error logs are separate from site logs and grow too
Get-ChildItem -Path "$env:SystemRoot\System32\LogFiles\HTTPERR" -Filter *.log -File -ErrorAction SilentlyContinue |
    Where-Object { $_.LastWriteTime -lt $cutoff } |
    Remove-Item -Force

For a quick one-off cleanup, this one-liner shows what would be deleted (remove -WhatIf to delete):

# Quick one-off: delete .log files older than 30 days under the default IIS log folder
Get-ChildItem "$env:SystemDrive\inetpub\logs\LogFiles" -Recurse -Filter *.log -File |
    Where-Object LastWriteTime -lt (Get-Date).AddDays(-30) |
    Remove-Item -WhatIf   # remove -WhatIf to actually delete

Schedule the Script

Create a daily task from an elevated command prompt:

REM Run the cleanup every day at 3:00 AM as SYSTEM
schtasks /Create /TN "Clean IIS Logs" /SC DAILY /ST 03:00 /RU SYSTEM /RL HIGHEST ^
  /TR "powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\scripts\CleanIisLogs.ps1"

Or use taskschd.msc: Create Basic Task > Daily > Start a program, with program powershell.exe and arguments -NoProfile -ExecutionPolicy Bypass -File "C:\scripts\CleanIisLogs.ps1". Run the task once from the list to check it works before its first scheduled run.

Other Ways To Keep IIS Logs Small

  • Compress the log folder: IIS logs are plain text and shrink dramatically with NTFS compression (folder Properties > Advanced > Compress contents).
  • Log fewer fields: in IIS Manager > Logging > Select Fields, untick fields you never use (cookies, referrer, user agent, and so on).
  • Move logs off the system drive: point the log directory at a data disk so a full log folder can't take down Windows.
  • Turn logging off for sites where you truly don't need it (Logging > Disable). Many sites rely on application logs and analytics instead.

Frequently Asked Questions

Does IIS delete old log files automatically?

No. IIS rolls over to a new file (daily by default) but keeps every old file forever. You need a scheduled script like the one above, or a log management tool.

How do I delete IIS logs older than 30 days?

Use PowerShell: Get-ChildItem the log folder for *.log files whose LastWriteTime is older than (Get-Date).AddDays(-30) and pipe them to Remove-Item. Schedule it daily with Task Scheduler.

What is the W3SVC folder?

Each IIS site writes its logs to a folder named W3SVC followed by the site's ID, such as W3SVC1 for the Default Web Site.

Summary

IIS logs grow without limit. A short PowerShell script and a daily scheduled task keep the last 30 days for every site and stop a full disk from breaking your applications.

Related: keeping IIS sites alive and automating SQL Server Express backups.


Comments

No Comments

CAPTCHA

Tagged: IIS


Join my email list!



ryan
About Me

With over 15 years in tech, I've excelled as a senior software engineer, specializing in ASP.NET, C#, SQL, Azure, and front-end technologies. I've led diverse projects across various sectors, from startups to global corporations, particularly during my decade in the San Francisco Bay Area.


Sign Up Hostinger Email