Automated test cleanup

Execution of test runs, either manuel or automatic, will create a bunch of diagnostic test data that can cause the the databse to crow quite rapid.

For vNext build /test jobs you define a retention policy, that will tell TFS how many days you want to keep the data (default 90 days). This job will run automatically every night.

But when using xaml based builds the retention policy is triggered on each build, so if you stop using a build definition, e.g. when you have relased and clone the definition, old builds and test data will be kept forever, until you trigger a build.

To cleanup the old test data, you can use the old “test attachment cleaner” from the Visual Studio 2015 power tools

When installed you will have some sample config files in C:\Program Files (x86)\Microsoft Team Foundation Server 2015 Power Tools\TestAttachmentCleaner_samples_settingsfile

Be aware of the LinkedBugs settings, that prevent it from delete test data from active bugs.

Example to delete all testdata older than 180 days and not on active bugs:

<AgeInDays OlderThan="180" />
<Attachment />
<Exclude state="Active" />
<Exclude state="Resolved" />

To automate it for every team project in a collecting, the following powershell script will iterate through all projects and run the cleanup:

$serverName = "http://fabrikam:8080/tfs"
$Collection = "defaultcollection"
$Mode = "Delete" #Delete/preview mode
$SettingsFile = "C:\Program Files (x86)\Microsoft Team Foundation Server 2015 Power Tools\olderthan180.xml"

# Remove log files older tham 14 days
get-childitem "C:\Program Files (x86)\Microsoft Team Foundation Server 2015 Power Tools\*" -Include *.log | where {$_.lastwritetime -lt (get-date).adddays(-14)} |% {remove-item $_.fullname -force}

#Get projects from collection
$projects = (Invoke-RestMethod -Uri "$($servername)/$($collection)/_apis/projects?api-version=1.0" -UseDefaultCredentials).value

foreach ($project in $projects)
[string] $projectName = $project.Name
Write-Host "Cleaning up TFS project '$projectName'"
$exe = "C:\Program Files (x86)\Microsoft Team Foundation Server 2015 Power Tools\tcmpt.exe"
&$exe attachmentcleanup /collection:$ServerName/$Collection /teamproject:$projectName /settingsfile:$SettingsFile /mode:$Mode

Author: KimC

TFS admin and deployment fellow

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: