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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can create a task that runs daily using Administrative Tools > Task Scheduler.
Set your task to run the following command:
forfiles /p "C:inetpublogsLogFiles" /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.
Method 2
One line batch script:
forfiles /p C:inetpublogs /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/
Method 3
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.
Method 4
Another viable Powershell one-liner:
Get-ChildItem -Path c:inetpublogslogfilesw3svc**.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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0