Create a Windows scheduled task that will delete the log files regularly:
- Save the script below as a vbs file on a local directory (i.e., delete_old_IISLogs_30days.vbs): On Windows Server 2008 and 2012, change the locations indicated on Call DeleteOldLogFiles to C:\inetpub\logs\LogFiles\ (default location).
Option Explicit
' Delete all IIS Log Files older than 30 days
Call DeleteOldLogFiles("C:\WINDOWS\system32\LogFiles\W3SVC3", 30)
Call DeleteOldLogFiles("C:\WINDOWS\system32\LogFiles\W3SVC4", 30)
Call DeleteOldLogFiles("C:\WINDOWS\system32\LogFiles\W3SVC5", 30)
Function DeleteOldLogFiles(strPath2Logs, intDays2Keep)
Dim fso, f, fc, f1, strFiles
strFiles = ""
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FolderExists(strPath2Logs)) Then
Set f = fso.GetFolder(strPath2Logs)
Set fc = f.Files
'-- Determine if file is older than defined days
For Each f1 in fc
If DateDiff("d", f1.DateLastModified, Now) > intDays2Keep Then
strFiles = strFiles & f1.Name & vbCrLf
f1.Delete(True)
End If
Next
Set f1 = Nothing
Set fc = Nothing
Set f = Nothing
End If
Set fso = Nothing
End FunctionA couple of changes was made especially in the "Delete all IIS Log Files older than 30 days" section. You can also change the logfile path and number of days (the current value is 30) based on your preferences. - On the OSCE server, go to Control Panel > Scheduled Task.
- Double-click Add Scheduled Task, follow the wizard and provide the following information:
- Browse the script that Windows will execute: delete_old_IISLogs_30days.vbs
- Task Name: Delete IIS Logs older than 30 days
- Set Schedule: Daily
- Set Start Time: 00:00
- Set Start Date: <Same Day>
- Enter Domain Admin Credentials
- Click Finish.
The scheduled task will run daily starting at 00:00 on the day of creation.