Deleting old IIS log files
The log files that IIS generates can, over time, consume a significant amount of disk space. Potentially, they can fill up your entire hard drive. To avoid this problem, you can turn off logging entirely. On the other hand, there are alternatives you can use to facilitate this issue:
Enable folder compression
Move the log folder to another system
Delete old log files by script
Delete old records by the IIS Log File Cleaner
In this article, we are going to discuss the third options because it represents the permanent solution.
Deleting old log files by script
There is a feature you can use to control the disk usage. In this case, you can run a script that automatically deletes data older than a certain age. Scheduling this script will keep your disc clean and under control, without ongoing maintenance.
The script will check the age of each log file in every folder, and it will delete all data older than a specific period. You can customize the script based on your needs, by changing the name of the patch folder in line 1 and changing the maximum age by adding the desired values in days in line 2.
How to run a script as a scheduled task?
Users can automate this entire task of cleaning log files by the script. You can schedule Windows task to start the script periodically, or you can use Windows Task Scheduler to run the script at any moment. Here are a couple of steps which can help you configure and run the script:
You should open Server Manager, click the Tools menu and then open Task Scheduler. In the Actions pane of the Task Scheduler, you will find a dialog box Create Task, click on that option. On the General tab of the Create Task, you should enter the name of the action you want to complete, for instance, Delete Log Files. Make sure to set all security priorities by choosing the user account with enough privileges to run the script.
Click the Triggers tab and then select New. In the New Trigger dialog box Begin the task to On a schedule. For example, you can choose it daily, insert the Start date, of you, can select more advanced options and settings to ensure that Enabled is checked if you are ready to start the schedule and click OK.
The next step is to click the Actions tab and then open New dialog. In the New Action dialog box, choose a value for Action. In your case, you will Strat a program. In Program/script, you should enter cscript and in Add arguments insert the path and name of the script file. To complete action, click OK.
Make sure that the task has been added to the Active Tasks pane and use the right click to select the Run option. Navigate to the folder that verifies that all files you have chosen have been deleted. To complete the action, click End so that status returns to Ready.