IIS 7 Log Files Auto Delete?

asp.netIis 7

asp.net Problem Overview


Is there any feature in IIS 7 that automatically deletes the logs files older than a specified amt of days?

I am aware that this can be accomplished by writing a script(and run it weekly) or a windows service, but i was wondering if there is any inbuilt feature or something that does that.

Also, Currently we turned logging off as it is stacking up a large amount of space. Will that be a problem?

asp.net Solutions


Solution 1 - asp.net

You can create a task that runs daily using Administrative Tools > Task Scheduler.

Set your task to run the following command:

forfiles /p "C:\inetpub\logs\LogFiles" /s /m *.* /c "cmd /c Del @path" /d -7

This command is for IIS7, and it deletes all the log files that are one week or older.

You can adjust the number of days by changing the /d arg value.

Solution 2 - asp.net

One line batch script:

forfiles /p C:\inetpub\logs /s /m *.log /d -14 /c "cmd /c del /q @file"

Modify the /d switch to change number of days a log file hangs around before deletion. The /s switch recurses subdirectories too.

Ref: http://debug.ga/iis-log-purging/

Solution 3 - asp.net

Similar solution but in powershell.

I've set a task to run powershell with the following line as an Argument..

dir D:\IISLogs |where { ((get-date)-$_.LastWriteTime).days -gt 15 }| remove-item -force

It removes all files in the D:\IISLOgs folder older than 15 days.

Solution 4 - asp.net

Another viable Powershell one-liner:

Get-ChildItem -Path c:\inetpub\logs\logfiles\w3svc*\*.log | where {$_.LastWriteTime -lt (get-date).AddDays(-180)} | Remove-Item -force

In case $_.LastWriteTime doesn't work, you can use $PSItem.LastWriteTime instead.

For more info and other suggestions to leverage the IIS LogFiles folder HDD space usage, I also suggest to read this blog post that I wrote on the topic.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCodingSlayerView Question on Stackoverflow
Solution 1 - asp.netIdo SelaView Answer on Stackoverflow
Solution 2 - asp.netjamacoView Answer on Stackoverflow
Solution 3 - asp.netPatoLocoView Answer on Stackoverflow
Solution 4 - asp.netDarksealView Answer on Stackoverflow