24

I just discovered that IIS builds up logs indefinitely and there don't appear to be any IIS settings that will automatically clean out old log files. What is the best way to keep my IIS logs under control so that they don't fill the entire hard drive?

3
  • do you want to keep the old ones around in a zip archive, or delete them? Sep 14, 2009 at 23:34
  • You are expected to manage the log files yourself. Oct 20, 2009 at 14:12
  • CCLeaner has an option to clean IIS Log Files !!!
    – user355506
    May 20, 2016 at 5:03

6 Answers 6

26

You'll have to run a scheduled task to do it. Here's a Powershell script that should work.

set-location c:\windows\system32\Logfiles\W3SVC1\ -ErrorAction Stop
foreach ($File in get-childitem -include *.log) {
   if ($File.LastWriteTime -lt (Get-Date).AddDays(-30)) {
      del $File
   }
}

This should purge anything that was last modified more than 30 days ago. Change the path in the first line to wherever your log files are stored. Also change the -30 to however long you want to retain the files. -30 means you will delete anything older than 30 days.

You can have a look at this article that shows different properties for the FileInfo object if you don't want to use LastWriteTime.

4
  • 2
    On Windows Server 2008 R2 (PS 2.0) I had to modify the Get-ChildItem call to be Get-ChildItem *.* -include *.log
    – roryWoods
    Jun 14, 2016 at 13:25
  • @roryWoods you can also use the -filter flag
    – H A
    Nov 8, 2022 at 16:38
  • I run a version of this that also zips them up incase, once zipped they are much smaller) Get-ChildItem -Path "D:\logs\www-iis\W3SVC3" -Recurse | Where-Object CreationTime -lt (Get-Date).AddDays(-7) | Move-Item -Destination "D:\logs\www-iis\temp" & "C:\Program Files\7-Zip\7z.exe" a D:\logs\www-iis\log-backup.7z D:\logs\www-iis\temp\* Remove-Item -path D:\logs\www-iis\temp\*
    – Jeff
    Dec 2, 2022 at 18:13
  • @Jeff 7z.exe comes with a -sdel switch to delete archived files afterwards, which makes it unnecessary to move them to a temporary folder and manually delete them. Feb 28, 2023 at 20:25
4

I'm currently doing this using a very simple batch file script:

forfiles -p C:\inetpub\logs\LogFiles\ -s -m *.log -d -180 -c "cmd /C DEL @File"

I also made a Scheduled Task entry to launch it daily and even activated the file compression feature on that folder: all these things together fixed my problem with IIS files for good.

Explanation of the switches in the batch file:

  • -s or /S : recurse into all subfolders
  • -p or /P : path
  • -m or /M : file mask
  • -d or /D : number of days (-180 = older than 180 days)
  • -c or /C : command to execute

If you're looking for a viable Powershell alternative, see this other answer: for other suggestions on how to properly reduce the IIS LogFiles folder, check out this post.

3

You could brew your own, but i believe some clever person has written this for you already. Check out IISLogs & IISLogs Lite!

If all your doing is deleting the logs, then you can turn logging off if its not needed! you will save your server a lot of I/O!

2
  • 2
    Just an update, but IISLogs Lite hasn't been available for some time, and IISLogs is not cheap.
    – Bacon Bits
    Jun 13, 2016 at 12:58
  • 1
    As of July 2021, IISLogs.com is a parked domain.
    – ssmith
    Jul 8, 2021 at 15:40
1

Well, if you want to clean them up regularly then why don't you disable request logging in IIS? You may use something like google analytics or some other service, I see many people dong this to avoid the headache with IIS logs affecting performance and eating up all disk space but it all depends on your requirements of course.

1

Microsoft suggests a script on it's website.

Below is a modified version I'm using for my needs.

Wscript.Echo "Starting log removal"
sLogFolder = "d:\retentiontest"

iMaxAge = 30   'in days
Set objFSO = CreateObject("Scripting.FileSystemObject")
set colFolder = objFSO.GetFolder(sLogFolder)
Wscript.Echo "Removing log files from folder: " & colFolder

For Each colSubfolder in colFolder.SubFolders
        Set objFolder = objFSO.GetFolder(colSubfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
                iFileAge = now-objFile.DateLastModified
                if iFileAge > (iMaxAge+1)  then
                        Wscript.Echo "Removing File: " & objFile.Name
                        objFSO.deletefile objFile, True

                end if
        Next
Next

Note that I've changed from "DateCreated" to "DateLastModified" since counterintuitively date created can be a later than last modified, as can be the case with copied files. You probably don't want to remove files that have been recently updated.

Then run it using cscript.exe (for example, cscript.exe d:\scripts\logRetentionScript.vbs).

1

Create a scheduled task on the server. In the actions section, you want:

  • Action: Start a Program
  • Settings: Program= Forfiles.exe (in Windows\System32 folder)
  • Add Arguments: -p "C:\inetpub\logs\LogFiles" -s -m . /D -45 /C "cmd /c del @path"
  • Start in: C:\

Give it a daily or weekly schedule. Done.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .