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 Next Next
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:
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.
- Open Server Manager, click the Tools menu, and then click Task Scheduler.
- In the Actions pane of the Task Scheduler dialog box, click Create Task.
- 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.
- 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.
- 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.
- Click OK.
- Verify that the task has been added to the Active Tasks pane.
- Right-click on the new task, and select Run.
- Navigate to the folder that the script ran on, and verify that the appropriate log files were deleted.
- 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.