How to keep IIS logs from filling up your server hard drives

IIS log files can take up a lot of space on your servers' hard drives if you don't clean them up regularly. But who has time for that? Check out this simple script to automatically delete IIS logs and clear space on your Windows servers.

If you're running a version of Microsoft Internet Information Services (IIS) on any of your servers, then you've

probably seen what happens when IIS logs are not cleaned up periodically. Old IIS logs can quickly eat up all of the free space on the hard drive, especially on servers with highly utilized websites, such as Microsoft Outlook Web Access or line-of-business applications.

Instead of having to check and clear IIS logs manually, wouldn't it be nice to have a scheduled task that deletes the old logs automatically? The solution is easily implemented with a simple Visual Basic script and a scheduled task on each of the servers you want to keep cleaned up.

To clear the old IIS logs, or any type of log file that accumulates over time, you'll need to first save the script presented below with the extension of .vbs, such as delete_old_logs.vbs. I like to put all of the maintenance scripts I place on a server in a specific directory that I create, which in my case is C:\Program Files\Scripts. But you can save the script file anywhere on the server.

After the script has been saved on the server, you'll need to edit the script to specify the exact path that is to be cleaned and how many days of log files should be left. You'll find this information in the line that starts with Call CleanDirectory. In the example below, the script is set to delete the default IIS logs that are older than seven days. Be sure your folder path is inside the quotation marks, and remember that you can insert multiple Call CleanDirectory lines if you'd like to delete logs from several sources on the same server simultaneously. After making your changes to the script, be sure to save the script file.

The last step is to schedule a task to run the script file. The account that you run the scheduled task under needs to have permissions on the folders where the log files are located, so it's usually best to create a special account (local or domain) and put that account in the local server administrations group. This will allow the script to perform its intended function in any location on the server.

When you create the scheduled task, you'll need to browse to the location where you saved the file. Make sure the path is enclosed in quotation marks if it contains any spaces. For best results, schedule the task to run daily.

With just a few simple steps, you can quickly implement a proactive solution on your servers to keep IIS logs from completely filling up your hard drives. Since Internet Information Services is not the only application that has a habit of creating lots of log files, I'm sure you'll find plenty of uses for this cleanup script.

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' delete_old_files.vbs
' Based on original script here:
'
http://www.microsoft.com/technet/scriptcenter/csc/scripts/files/files/
cscfi006.mspx
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

' Clean the listed directories of old backup files
Call CleanDirectory("C:\WINDOWS\system32\LogFiles\W3SVC1", 7)

' Delete old backup files
Function CleanDirectory(strPathtoFiles, intDaystoKeep)
   Dim fso, f, fc, f1, strFiles, intFiles
   strFiles = ""
   intFiles = 0

   Set fso = CreateObject("Scripting.FileSystemObject")
   If (fso.FolderExists(strPathtoFiles)) Then
      Set f = fso.GetFolder(strPathtofiles)
      Set fc = f.Files

      '-- Determine if file is older than defined days
      For Each f1 in fc
         If DateDiff("d", f1.DateLastModified, Now) > intDaystoKeep Then
            strFiles = strFiles & f1.Name & vbCrLf
            intFiles = intFiles + 1
            f1.Delete(True)
         End If
      Next

      Set f1 = Nothing
      Set fc = Nothing
      Set f = Nothing

   End if

   set fso = Nothing

End Function

' Stop the script in case it doesn't automatically WScript.Quit

ABOUT THE AUTHOR
Will Schmied
is a senior systems administrator for a world renowned children's research hospital. He holds numerous Microsoft MCITP, MCTS and older certifications and is experienced with Exchange and Blackberry. Will has been actively involved with the certification and training side of IT for many years, writing or contributing to several dozen books. He is a founder of the certification portal MCSE World. Having passed the reigns to a good friend from down under, Will maintains a smaller presence today with his blog, Tales of a Systems Administrator.

This was first published in March 2009

Dig deeper on Microsoft Windows Data Backup and Protection

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

1 comment

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close