Prevent IIS log files from using too much disk space on Windows Server

Compress and truncate log files to save disk space

Cypress North

Have you ever noticed that an IIS server seems to slowly eat disk space over time? If you're hosting many sites, or a few high traffic sites, it might not be that slow to consume a big portion of your hard drive. You check the usual culprits like database log files, temp files, etc. but the unassuming IIS web log could be to blame.

By default, each IIS Site hosted will begin recording web logs into %SystemDrive%\inetpub\logs\LogFiles and it will create a new file every day. For some sites, this isn't a big deal as the file might only be 10KB - 1000KB in size, but for others, each of these files can balloon to over 100MB per day. 

logs Cypress North

This can become a problem pretty quick if you're storing your log files on the system drive, in fact it's recommended that you don't do that, but of course it's happening out there. To exacerbate the issue, the log file history is not truncated, it will just keep creating a new file every day forever. I logged into an IIS server the other day to find that the logs had grown to over 50GB. There are a couple of steps you can take to avoid this problem.


One measure you can take is to compress the log directory. If you need to retain all of your log history this is a helpful option. Since the log files are just a line of text per record, their file size can be greatly reduced with compression. 

To automatically compress all of your log files and any new ones, right click on the LogFiles folder in Widows Explorer and choose Properties. Then click the "Advanced" button toward the bottom. On the next screen, check the box to "Compress contents to save disk space". This can take some time if you have a lot of logs.

compress Cypress North

Automated Truncation

A longer term solution is to determine how many days of logs you actually need to retain and periodically delete the logs older than that. In my situation, I don't really need more than 30 days of logs, so I can delete everything older than 30 days.

The first part of this solution is to create a VB Script to delete the files. 

sLogFolder = "c:\inetpub\logs\LogFiles"
iMaxAge = 30   'in days
Set objFSO = CreateObject("Scripting.FileSystemObject")
set colFolder = objFSO.GetFolder(sLogFolder)
For Each colSubfolder in colFolder.SubFolders
        Set objFolder = objFSO.GetFolder(colSubfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
                iFileAge = now-objFile.DateCreated
                if iFileAge > (iMaxAge+1)  then
                        objFSO.deletefile objFile, True
                end if

Edit this script to point the sLogFolder to your actual log directory and the iMaxAge variable to the number of days you'd like to retain logs for. The script will recursively loop through the LogFiles directory and delete files older than your threshold.

You can execute this script manually by running the following from a command prompt:

cscript.exe c:\path-to-your-scripts\scriptname.vbs

Finally, to complete the automation, set up a scheduled task to execute that script. Using the Windows Task Scheduler, create a new task and set the Trigger to execute daily (or whatever interval makes sense for you). Create a new Action that looks like the following:

action Cypress North

Just put the path to your script file inside the Arguments textbox.

And that's it! Your log files will now be kept in check and you'll avoid the slow drain of free disk space common to IIS server installs. 


This story, "Prevent IIS log files from using too much disk space on Windows Server" was originally published by ITworld.

Copyright © 2014 IDG Communications, Inc.

Shop Tech Products at Amazon