Release: Build Clean-up Service for Team Foundation Server
22 05 2006The Team Build facility in Team Foundation Server allows development teams, and in particular configuration managers to define a build that can be performed on a remote server at the click of a button. After the build is complete the results of that build process are uploaded to a drop location on a network volume somewhere.
Builds that are performed can quite often be rejected because of a failure caused by a file not being checked in, or some debug-time setting propogating into a build destined for release. Other builds may be rejected at a later date due to functional testing identifying a problem.

While recording details about each build that is performed is useful from an auditing perspective, storing the actual artefacts from that build does come at a cost – a storage cost. The solution is to regularly clean the old rejected builds out of the drop folder to reclaim space.
At Readify we try to run a pretty tight ship when it comes to Team Foundation Server. So not just anybody can go in and delete files from the drop location – that might remove good builds that may be required for diagnostics later on down the track. But how do we allow people clean-out the garbage?
Introducing the Build Clean-up Service
This is an extension to Team Foundation Server that I’ve had queued up to develop for quite some time. The clean-up service takes care of cleaning out the “Rejected” and “Unexamined” builds after a specified period of time (configurable).
After installing and starting the build clean-up service a few things happens. First, the “WorkItemStore” is contacted to get a list of Team Projects defined within Team Foundation Server, then a list of builds performed under that scope of that Team Project is retrieved from the “BuildStore”.
Each of the builds is checked in turn to determine whether it is a candidate for clean-up. The rules for determining this are as follows:
- Has the expiry date for the build been reached? This is a basic calculation to determine whether time since the build was finished is greater than the expiry time span defined in the configuration file. Currently ours is set to fifteen days.
- Does the drop location exist? This feature was added after I realised that during the installation of our Team Foundation Server I had tested out some builds and subsequently deleted them off the file system – this will be a pretty common scenario I think.
- Has the build actually completed? If you set quite a low expiry time span and you happened to have a fairly long build process its concievable that the service could kick in and start trying to delete the build while it was still running. So here we make sure we are only considering whether a build is a candidate for clean-up checks for this.
- Has the build been abandoned? Abandoned build are builds that are either in a Rejected or Unexamined state. By default all builds go into an Unexamined state, but remember that the build needs to have expired as well before it gets cleaned-up so you shouldn’t have good builds going missing before you have a chance to inspect them unless you set a really low processing interval on the service (default is one hour).
Once it has been determined that a build is a candidate for clean-up the service systematically deletes the build artefacts in each of the specified build locations. There are a few things that it doesn’t touch however. The clean-up service will not delete the build log (provided it is called BuildLog.txt) and it will wrote a “Readme.txt” file to explain what happened to the files.
In addition to that, two entries will be placed in the event log, one for the start, and one for the completion of the clean-up for each build. This provides an audit trail that monitoring software can pick up if you require that level of control.

Downloading and Installing the Build Clean-up Service
Before you download, install and run the build clean-up service I want to let you know that I take no responsibility for what this does to your system. It was tested on my laptop and a live Team Foundation Server environment but I can’t guarantee that I’ve picked up every issue. I recommend that you take a backup of your drop locations prior to installing and running this service – just in case, and I would warn development teams that it is being turned on. That said – I’m pretty sure that it’ll work for you.
The first thing that you need to do is download and unzip the service onto a machine that has Team Foundation Server components installed on it. I would recommend either the application tier or the team build server (if you have those components distributed). After that you need to use the .NET Framework (2.0) installation utility to register the service on the computer:
InstallUtil.exe BuildCleanup.exe
During the installation process it will prompt for credentials for the service. One sure fire way to make sure it has the right credentials is to make it run under the same service account as the Team Build service, on most systems thats [DOMAIN]\TFSBUILD.
The final step is to edit the BuildCleanup.exe.config configuration file to point the service at the URL for your Team Foundation Server. Once that is done you should be able to start the service by issuing the following command:
net start BuildCleanupService
When the service springs to life you’ll probably get a flurry of activity, depending on how long your Rejected and Unexamined builds have been sitting around for. If you want to get a picture of what the service is doing just look at the event log on the system it is running on.
What’s next?
You tell me! If you have any features that you would like to have added to the Build Cleanup Service then shoot me an e-mail to let me know. I’ve got a few that I would like to see:
- A Windows Installer-based setup package which includes prompts for setting up things like expiry time spans, processing intervals and the Team Foundation Server URL.
- Warning e-mails for those that iniated builds that are going to be deleted.
- Support for changing the build quality indicator to “Deleted”.
- More robust error handling and logging.



Sir
I have a similar requirement as of Clean Up Service. But in addition, I also need to delete the code from build location. I am not able to locate any method in “Build Store” , which provides BuildDirectoryPath.
Please advise for this.
Regards
Anurag
Hi Anurag,
Why do you need to delete those files - won’t the build just put them right back again?
Hi,
I found this service very useful, however I have two questions:
1. I’m using one build server for several builds of three independent projects, so each project has its own drop location. How can I tell the CleanUp service to check in all drop locations?
2. How can I uninstall the service?
Thank you,
Zevik
Hi Zevik,
The cleanup server uses the BuildInfo object to determine where the drop folder is so it is completely dynamic. As long as the clean-up server can get there it will clean it up.
The service can be un-installed using InstallUtil.exe:
installutil.exe -u [exe file name]
Hi Mitch,
Thank you,
I’ve installed this service on build server, but it doesn’t clean builds.
Here some parameters that I checked:
I’ve changed the URI in config file to http://builder:8080
In services i see that BuildCleanUpSevice is started and running.
I can see in event viewer that service has started successfully also.
When I’m trying to insert this URI in IE i get: “You are not authorized to view this page”, I’ve tryed different accounts (administrator, TFSService).
What I’m doing wrong?
When this service scheduled to start checking the builds?
How to understand the format of CleanupInterval, days.???:hours:minutes?
Thank in advance,
Zevik
Hi Zevik,
The URI in the config file should be your TFS Server URL. The default configuration is for the unexamined builds to be cleaned out after 15 days (15.00:00:00), and the service checks to see if files have disappeared every hour (1:00:00). You should see some events in the event log when it happens.
The format for the time span is days.hours:minutes:seconds
Hi Mitch,
It’s working well now!
The problem was that I’ve inserted Build URL instead of TFS.
Thank you,
Zevik
Hello,
I’m having an issue that is similar to the problem Zevik described in post #5. The TFS Build is instaled on the same machine as TFS itself. Because of this, I am using the TFS service account (TFSSERVICE) for the build service account as well. When trying to start the service I am receiving this error:
System error 5 has occurred.
Access is denied.
I’ve been trying to figure out what permissions may be wrong, but I was wondering if you had any ideas in that regard. I should also note that because both TFS and TFS Build are running on the same machine, the URL I am using is http://localhost:8080
Hi Matt,
The build clean-up service needs delete access to the drop folder. Can you make sure that the ACLs are set correctly on both the share and the filesystem.
[...] Delete builds during the build Now that is an intriguing title. Buf if you are using Team Foundation Server and especially Team Build you know where I’m talking about. If you use the team build as the heart beat of your project and therefore build daily (or nightly if you’re in the java camp) you end up with a large list of Builds. Most of them are probably failed (just kidding), some of them have a build quality of unexamined and most of them are so old you probalby never want to touch or install any of it. So you want to get rid of the old builds. Luckily you can. Microsoft provided us with a nice command line utility called tfsbuild which enable you to remove completed builds. Just provide a projectname and a buildnumber. Repeat 400 times until all unwanted builds are deleted. There must be an easier way. I already found this solution but in our development shop I’m not the owner of the buildbox. Handing over the control of which builds to delete to an uncontrolled service didn’t appealed to me much. So reinvent the wheel to come up with a for me more suitable solution. Create a msbuild Task that deletes builds based on criteria that are provided in the build script. In the download you’ll find the source (so you can see that there is no virus in it) of the build task that enables you to delete builds from the BuildStore. If you place the compiled assembly in the same source control folder where tfsbuild.proj resides you can start configuring. In your tfsbuild.proj script do this: <UsingTask AssemblyFile=”DeleteBuild.dll” TaskName=”DeleteBuilds” /> <Target Name=”AfterEndToEndIteration”><DeleteBuilds TeamServer=”[your tfs server]” <!– tfs:8080 –> TeamProject=”$(TeamProject)” BuildType=”$(BuildType)” Status=”Failed” Quality=”Unexamined” Days=”20″ /></Target> Check-in all your changes, kick-off your build and you’ll see the builds disappear as snow for the sun. Having the Days as selection mechanism quarantees that you do not have to modify the script day after day. If you come up with a good set of thresholds you can leave it running without any aftercare. Best of all is that not only you’re builds from the BuildStore will disappaer but also the files on you’re buildserver will be removed, so it really cleans up. This attached source code shows the basic mechanism. I have a more advanced version available. That one will also be able to run as service (as mentioned earlier) and has a more sophisticated query mechanism. Just contact me if you’re intereseted in that version. Published Thursday, December 07, 2006 8:06 PM by Rene Schrieken Filed under: Team Foundation Server Attachment(s): DeleteBuilds.zip [...]
Hi,
I insalled ur service,now when I made changes in the supplied BuildCleanup.exe.config accroding to my needs ,how the service will read this file ??????????
Or we need to copy this file to some location?????????????????
Where this file should be present to be read by the service?????????
Hi Sarabjit,
The file just needs to be in the same directory as BuildCleanup.exe, it is the default configuration file name.
The config file is there only in the same directory as exe is present,but it is not reading the enteries from the config file.What can be the reason??????
Hi Sarabjit,
Did you reboot the service?
Hi Mitch,
I am trying to run the cleanup process to leave the last 2 successful builds and possibly 1 last failed one to determine changes required for the subsequent successful build. Is there a way to remove builds from a certain time frame only? We are carrying out hourly builds and would like to retain the last 2 successful builds at any point of the day. Any suggestions?
Hi Adam,
At present the build clean-up service doesn’t support this so you would need to implement it yourself using the TFS SDK.
Hi Mitch,
I’ve been wanting to try this out, but the download link isn’t working…
Hi Howard,
Just in the process of moving some data around. should be available within a week.
Hi Mitch,
I’ve had this service running several months now without a hitch. (great job - thanks!) Lately however, I’ve noticed event log errors:
The following exception occured while trying to perform a build clean-up:
System.Web.Services.Protocols.SoapException: TF50322: Unrecognized project name.
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at Microsoft.TeamFoundation.Build.Proxy.BuildStore.GetListOfBuilds(String teamProject, String buildType)
at BuildCleanup.BuildCleanupService.GetBuilds(Project project)
at BuildCleanup.BuildCleanupService.GetAllBuilds()
at BuildCleanup.BuildCleanupService.m_Timer_Elapsed(Object sender, ElapsedEventArgs e)
My assumption is that you are using the ListAllProjects service call and then calling BuildStore.GetListOfBuilds for each one returned. I did this manually and received no errors from either web service. So I can’t figure out why this is happening. Any ideas?
Hi Tim,
Seems to be some kind of corruption in the system somewhere - I wouldn’t expect that error. Have you recently deleted a Team Project?
Oh yes - I’ve deleted many team projects.
What service calls are you making in order to do the cleanup? Any chance I can see some of the source, as a way to help me identify what’s wrong with my environment?
BTW - if you email me when you respond, I won’t need to keep checking here every couple of days. Can you do that?
Hi,
Doesnt it delete the rows in the list of builds?
My list is soo slow because there are so many rows, and I was hoping your tool would cleanup the list as well?
Please reply to my email….
/Niels
[...] http://notgartner.wordpress.com/2006/05/22/release-build-clean-up-service-for-team-foundation-server... [...]