Automatic IIS log removal

Having seen the data used on the C:\ drive of a number of Windows Small Business Server 2008 machines slowly spiral out of control, reducing the free space to only 1-2Gb on 50Gb+ drives I started to suspect something strange was happening. With the use of a handy tool named Tree Size which is available as a free download, I soon discovered it was the IIS logs that had been hogging all the space. IIS itself, performs no log rotation. The only way to control them is with the use of some VBS script.


Personally, I didn’t need to keep the logs longer than 30 days and found the following script ideal for deleting the excess logs when setup to run as a scheduled task:

Option Explicit
WScript.Timeout = 82800

' This Script deletes IIS log files older than a specified number
' of days.
'
' Run it as a daily scheduled task on high traffic web servers to
' avoid running out of disc space.
'
' The DeleteLogFiles function takes two parameters:
' "Path to log dir"
' "Delete log files older than n days"
'
' Multiple function calls can be added to delete files in different
' log folders with different log retentions.
'
' Note that the function runs through subfolders recursively, so if
' the same log retention should be used on a whole log folder tree
' structure, only one call with the root log folder is needed.
' Additional calls with specific subfolders can then be made to have
' shorter retentions on those.
'
' Edit the example lines below to match the log folder paths and
' retention values needed on the server.

DeleteLogFiles "D:\Logfiles", 30
DeleteLogFiles "D:\Logfiles\W3SVC1", 14
DeleteLogFiles "D:\Logfiles\W3SVC243", 5
DeleteLogFiles "D:\Logfiles\SMTPSVC1", 7

Function DeleteLogFiles(strLogPath, intDelAge)
Dim objFs
Dim objFolder
Dim objSubFolder
Dim objFile
Dim objWShell
Set objWShell = CreateObject("WScript.Shell")
Set objFs = CreateObject("Scripting.FileSystemObject")
If Right(strLogPath, 1) <> "\" Then
strLogPath = strLogPath & "\"
End If
If objFs.FolderExists(strLogPath) Then
Set objFolder = objFs.GetFolder(strLogPath)
For Each objSubFolder in objFolder.subFolders
DeleteLogFiles strLogPath & objSubFolder.Name, intDelAge
Next
For Each objFile in objFolder.Files
If (InStr(objFile.Name, "ex") > 0) _
And (Right(objFile.Name, 4) = ".log") Then
If DateDiff("d",objFile.DateLastModified,Date) > intDelAge Then
objFs.DeleteFile(strLogPath & objFile.Name)
End If
End If
Next
Set objFs = Nothing
Set objFolder = Nothing
Set objWShell = nothing
End If
End Function

Thanks to the guy(s) at 808.dk for the initial code. There are other scripts available there if you want to archive your logs as well.

This entry was posted in IIS, Microsoft, Windows Server and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette