Examining Sitecore 6.5 CleanupAgent

I bumped into this issue that logfiles were not cleaned up after X days. In Sitecore there is an agent that handles cleanup (Sitecore.Tasks.CleanupAgent). The following basic setup is specified in the web.config

If you take a look at the comment specified just above the <files> node, there is not really a clear definition on how to specify the cleanup. Except a bit of guessing what it does. When we look at [maxAge] it is pretty clear what it does. It simply checks if the has exceed the age specified in [maxAge] (see example below).
But what about [minAge]? What is the business rule? In this article I will explain every property that can be set on how it works.
Let's examine what is written on the comment of what we can know so far.

# If we specify rolling="true", we ignore the property "minCount and "maxCount"
# [minAge] and [maxAge] must me specified as [days.]hh:mm:ss. So there is a specify format.
# The default value for [minAge] is 30 minutes.
# [strategy] specify the number of files will be processed within hour, day, week month, year
# If we specify [recursive=true], it will look in folders within the specified folder.

To really understand what is happening with these properties. I have decompiled the Sitecore.Kernel.dll to see what's underneath the hood on the workings of the CleanupAgent.

To start simple, we start examining the [maxAge] and [recursive]. After that we take a look at [minAge], [minCount] and [maxCount] and then the [rolling] property along with the [strategy] property.

Example [maxAge]
To give you an example on how the [maxAge] works A file was modified or created (it first looks on modified) 2 weeks ago and we specified the [maxAge] to 7 days, the file will be removed. But if we specify [maxAge] to 30 days, it will not be removed.
Adding [recursive]
When we append the [recursive] property with the value of "true", it will look in descendant folders and files. So if we specify these and we have a subfolder within the folder we specified, it will also look in there.
You can use [maxAge] for defining the maximum age of a file and [recursive] to look deeper than just the main folder.
What about [minAge]
When a file is within the [maxAge] rule, the file will be added to a list and the amount of items in that list will be checked against the [minCount] property (default = 0) if the amount of items exceeds the [minCount], then a check with the amount of items will be done against [maxCount] property, it substracts the [maxCount] from the total amount of items and checks if the result is greater than 0. When this property is NOT defined, it will use the Int32.MaxValue. 
Full example of [maxAge], [recursive], [minAge], [maxCount], [minCount]
[maxAge] => 90.00:00:00
[minAge] => 30.00:00:00
[recursive] => true
[maxCount] => 15
[minCount] = 0

Total files: 50 in main folder, 250 in subfolders.
Total files within [maxAge]: 200
Total files within [minAge]: 50
Now the it goes as follows:
100 files outside [maxAge] => delete
200 files inside [maxAge]
numberToProcess = 200 files - [maxCount] => 185
for each numberToProcess (185), check if it is below [minAge], if yes => remove. => 50 files deleted
135 files left.
The [rolling] property
As in the comment, it ignores the [minCount] and [maxCount] property. It removes files that matches the [strategy] property. It takes that selection and then checks each file with the [minAge] property. Also, the [recursive] property here is supported.

The strategy goes as follows:

hour => file modified within 1 hour
day => file modified within 1 day
week => file modified within 7 days
month => file modified within 30 days
year => file modified within 365 days

If the file exceeds 365 days, it will be removed directly.

I hope this article has been helpfull for you and gave you a clear understanding on how the CleanupAgent works with the properties that can be defined on every folder. Feedback is welcome, please use the comment box or mail.

By Stephan, Contributers: Jelle Overmars / 1/28/2014 / 5018 Views

Leave a reply