Sitecore Quick Tip: Recursively Delete Old Log Files
By default, Sitecore sets the deletion of log files to 30 days but does not recurse the deletion. This may leave logs in place older than 30 days. Here is the section of the default Sitecore.config file that houses the Sitecore.Tasks.CleanupAgent:
<!-- Agent to clean up work files -->
<agent type="Sitecore.Tasks.CleanupAgent" method="Run" interval="06:00:00">
<!-- Specifies files to be cleaned up.
If rolling="true", [minCount] and [maxCount] will be ignored.
[minAge] and [maxAge] must be specified as [days.]hh:mm:ss. The default value
of [minAge] is 30 minutes.
[strategy]: number of files within hour, day, week, month, year
[recursive=true|false]: descend folders?
-->
<files hint="raw:AddCommand">
<remove folder="$(dataFolder)/logs" pattern="*log.*.txt" maxAge="30.00:00:00" />
<remove folder="$(dataFolder)/diagnostics" pattern="*.*" maxAge="30.00:00:00" recursive="true" />
<remove folder="$(dataFolder)/viewstate" pattern="*.txt" maxAge="2.00:00:00" recursive="true" />
<remove folder="$(tempFolder)/diagnostics" pattern="*.*" maxAge="00:10:00" recursive="true" />
<remove folder="/App_Data/MediaCache" pattern="*.*" maxAge="90.00:00:00" recursive="true" />
</files>
</agent>
Note that this line is missing the recursive=”true” parameter.
<remove folder="$(dataFolder)/logs" pattern="*log.*.txt" maxAge="30.00:00:00" />
A patch file to remediate this issue and ensure recursive deletion is as follows. Use this to not only control recursive deletion and how often the CleanupAgent runs… but how long to keep log files, media cache, and other “work files”.
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<!-- Agent to clean up work files -->
<agent type="Sitecore.Tasks.CleanupAgent" method="Run" interval="06:00:00">
<!-- Specifies files to be cleaned up.
If rolling="true", [minCount] and [maxCount] will be ignored.
[minAge] and [maxAge] must be specified as [days.]hh:mm:ss. The default value
of [minAge] is 30 minutes.
[strategy]: number of files within hour, day, week, month, year [recursive=true|false]: descend folders?
-->
<files hint="raw:AddCommand">
<remove folder="D:\home\site\wwwroot\App_Data/logs" pattern="*log.*.txt" maxAge="30.00:00:00" recursive="true"/>
<remove folder="D:\home\site\wwwroot\App_Data/diagnostics" pattern="*.*" maxAge="30.00:00:00" recursive="true"/>
<remove folder="D:\home\site\wwwroot\App_Data/viewstate" pattern="*.txt" maxAge="2.00:00:00" recursive="true"/>
<remove folder="d:\local\temp/diagnostics" pattern="*.*" maxAge="00:10:00" recursive="true"/>
<remove folder="d:\local\MediaCache" pattern="*.*" maxAge="90.00:00:00" recursive="true"/>
</files>
</agent>
</scheduling>
</sitecore>
</configuration>
Please note that the above has a pattern to only remove log and txt files for the logs directory, so if you also want to remove the containing folders instead of leaving them empty, you will need to change your pattern match for log removal to:
pattern="*"