How can I keep IIS log files cleaned up regularly?

Solution 1:

You'll have to run a scheduled task to do it. Here's a Powershell script that should work.

set-location c:\windows\system32\Logfiles\W3SVC1\ -ErrorAction Stop
foreach ($File in get-childitem -include *.log) {
   if ($File.LastWriteTime -lt (Get-Date).AddDays(-30)) {
      del $File
   }
}

This should purge anything that was last modified more than 30 days ago. Change the path in the first line to wherever your log files are stored. Also change the -30 to however long you want to retain the files. -30 means you will delete anything older than 30 days.

You can have a look at this article that shows different properties for the FileInfo object if you don't want to use LastWriteTime.

Solution 2:

You could brew your own, but i believe some clever person has written this for you already. Check out IISLogs & IISLogs Lite!

If all your doing is deleting the logs, then you can turn logging off if its not needed! you will save your server a lot of I/O!

Solution 3:

I'm currently doing this using a very simple batch file script:

forfiles -p C:\inetpub\logs\LogFiles\ -s -m *.log -d -180 -c "cmd /C DEL @File"

I also made a Scheduled Task entry to launch it daily and even activated the file compression feature on that folder: all these things together fixed my problem with IIS files for good.

Explanation of the switches in the batch file:

  • -s or /S : recurse into all subfolders
  • -p or /P : path
  • -m or /M : file mask
  • -d or /D : number of days (-180 = older than 180 days)
  • -c or /C : command to execute

If you're looking for a viable Powershell alternative, see this other answer: for other suggestions on how to properly reduce the IIS LogFiles folder, check out this post.

Solution 4:

Well, if you want to clean them up regularly then why don't you disable request logging in IIS? You may use something like google analytics or some other service, I see many people dong this to avoid the headache with IIS logs affecting performance and eating up all disk space but it all depends on your requirements of course.