Purging Old IIS Log Files

In News, Servers by jbcdigital

Over time, the log files that IIS generates can take up a large amount of disk space – even completely filling the disk drive. Unfortunately, IIS doesn’t have a built in purge of log files that are past a certain date.  By default, IIS rolls a new log file every day, which, as stated can fill the disk with old files.  To mitigate this problem, we recommend creating and scheduling a script to run that would purge old files.

The following VBScript will check the age of each log file in a folder and will delete any log file older than a specified age. To customize the script, simply change the name and path of the folder in line 1 of the script, and change the maximum age to the desired value in days, in line 2.

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

The script above will scan all subfolders, so it will process logs for ALL sites in and under the folder specified. If you want to limit the process to just a single site, change the path appropriately.

To run the script manually, execute the following script in an administrator command line:

cscript.exe c:\scripts\retentionscript.vbs

Using a script to delete log files is a long-term, reliable solution to the problem of log files consuming disk space. If you automate the process, as shown below, it doesn’t require constant maintenance.

Run the script as a scheduled task

You can automate the task of deleting log files by script by creating a Windows task schedule to run the script periodically. You can schedule the script to run at any time using the Windows Task Scheduler. How you configure the scheduled task should be coordinated with the configuration of the log file rollover options.

  1. Open Server Manager, click the Tools menu, and then click Task Scheduler.
  2. In the Actions pane of the Task Scheduler dialog box, click Create Task.Create Task control
  3. On the General tab of the Create Task dialog box, enter a name for the task, such as “Delete Log Files”. Set the security properties, selecting a user account with sufficient privileges to run the script.Create Task dialog box
  4. Click the Triggers tab, and then click New. In the New Trigger dialog box, set Begin the task to On a schedule. Select the periodicity, for example, Daily. Enter the Start date, select more advanced settings, and ensure that Enabled is selected if you are ready to initiate the schedule. Click OK.New Trigger dialog box
  5. Click the Actions tab, and then click New. In the New Action dialog box, select a value for Action, in this case, Start a program. In Program/script, enter cscript, and in Add arguments (optional), enter the path and name of the script file, for example, “C:\iis\Log_File_Deletion.vbs”. Click OK.New Action dialog box
  6. Click OK.
  7. Verify that the task has been added to the Active Tasks pane.
  8. Right-click on the new task, and select Run.Task Scheduler dialog box
  9. Navigate to the folder that the script ran on, and verify that the appropriate log files were deleted.
  10. Navigate back to the Task Scheduler, right-click on the task, and click End so the status returns to Ready and the task is ready for scheduled runs.


Consolidated from http://www.iis.net/learn/manage/provisioning-and-managing-iis/managing-iis-log-file-storage