Click here to Skip to main content
15,613,716 members
Articles / Desktop Programming / XAML
Posted 21 Jan 2013


7 bookmarked

TFS: Stop running out of disk space on your build servers

Rate me:
Please Sign up or sign in to vote.
4.86/5 (5 votes)
7 Feb 2013CPOL3 min read
A TFS build template that cleans up after itself once the build is complete


A simple modification to the TFS default build template to allow a build to clean up after itself when complete by deleting its build directory on the build server. If you are not familiar with customising TFS Build or using Windows Workflow then this article is a nice introduction.


For small TFS setups it in unlikely we would ever fill up our build servers with source code to the point that they run out of disk space but in a large enterprise environment this is certainly possible.

In my current team we have a farm of build servers running up to 150 builds a day from 50 distinct build definitions across many team projects. Each source directory, once compiled, built and tested, is in the order of 2GB and this means we can easily fill up our build server hard disks if we are not careful.

This simple modification to the standard build template allows the build to delete it's source, binaries and test directories on the build server once the build completes and puts this functionality on a switch so you can control which builds do this and which do not.

Full example source is available and written in VS 2010 and against TFS 2010, but is equally applicable to TFS2012

Using the code 

TFS Build is written using Windows Workflow and customising the build process usually involves editing the .xaml workflow file using the workflow editor in Visual Studio. We will start with the standard 'DefaultTemplate.xaml' build template provided with TFS 2010 and go from there.

Step 1) Adding the delete directory task to the workflow

TFS provides a ready to use 'DeleteDirectory' activity. Just simply drag and drop it into the end of the 'Run On Agent' section and set the directory to delete as BuildDirectory

Image 1

Thats it (almost). This will delete the build directory on the build server once the build completes but theres more to do yet 

Step 2) Ensure the directory is always deleted

If the build fails to compile or encounters any other error then the workflow will throw an exception and the 'Delete Build Directory' task will be missed. We solve this by using a Try\Catch\Finally block which is a standard tool in the workflow toolbox

Image 2

This ensures we always delete the build directory even if an error occurs during the build process.

Step 3) Create the UI

Sometimes it is useful to examine the build directory after a build to diagnose any errors. We will provide an argument and UI to the build workflow so we can control when we delete the build directory.

First thing is to add a new boolean argument DeleteBuildDirectory to the workflow file

Image 3

Then we will update the Metadata, this will setup the UI so we can set this new argument through the Visual Studio UI. The Metadata is accessed through the ellipsis button highlighted above. We want to edit the dialog so it looks something like this. This adds a new option under the 'Basic' subsection of the build definition to control our new argument.

Image 4

Finally, add an if/else activity to the workflow to only delete the directory if our new argument DeleteBuildDirectory is true

Image 5

The result is a parameter you can set on the build definition

Image 6

and we have a build template that cleans up after itself. You can see the directory being cleaned up in the build log, right at the end.

Image 7


05/02/2013 - Minor modification to source


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
United Kingdom United Kingdom
I like to code and work mainly in .NET although i'm moving into other areas nowadays.
When i'm not coding I like to ride my bike

TFS Productivity Pack


Comments and Discussions

QuestionRunning into directory not empty issue on post cleanup Pin
Member 142085861-Apr-19 18:47
Member 142085861-Apr-19 18:47 
QuestionSome process not exist in the article Pin
Salih KARAHAN15-Feb-17 21:37
professionalSalih KARAHAN15-Feb-17 21:37 

You don't show how to set BuildDirectory value or i can't D'Oh! | :doh:
QuestionExcellent , very helpfull ... I saved 150 GB Pin
Anas Ghanem18-Feb-15 22:41
Anas Ghanem18-Feb-15 22:41 
QuestionCan this be modified to only delete the directory on a successful build? Pin
mist8325-Jan-13 4:32
mist8325-Jan-13 4:32 
AnswerRe: Can this be modified to only delete the directory on a successful build? Pin
Shining Dragon28-Jan-13 23:52
Shining Dragon28-Jan-13 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.