MSBuild
Deleting TFS builds

If you ever wanted to write a C# utility which allows you to delete more than one tfs build at the same time you'll love me for this....

TFS TeamPlain will allow you to delete one build at a time. Visual Studio's Team Explorer for TFS 2008 now lets you delete multiple builds at the same time ..what's also cool is that you can configure the builds you will like to preserve so say, only store the last 2 failed builds and the last 2 stopped builds but preserve all successful builds.  That way you may never need a utitlty to clean up your builds...

However, like me, you may want to write a utility that cleans up the builds should you wish to preserve more or if you don't have team server 2008.....and perhaps schedule it to run as a service. In any case, what you need to do is....

  • Write a filter which takes in specific search criteria so that you can return and act on builds that match the specified criteria.

List<BuildData> buildsToAdd = new List<BuildData>();
TeamFoundationServer tfs = new TeamFoundationServer(this.tfsDetails.ServerName);
BuildController controller = (BuildController)tfs.GetService(typeof(BuildController));

BuildStore bs = (BuildStore)tfs.GetService(typeof(BuildStore));
BuildData[] builds = bs.GetListOfBuilds(this.tfsDetails.Project, this.tfsDetails.BuildType);

  • Then write the code to delete the builds. I stuck the result in a checkedListBox so that the user was able to select which of the builds from the result he'd like to delete.  

         TeamFoundationServer tfs = new TeamFoundationServer(this.tfsDetails.ServerName);
         BuildController controller = (BuildController)tfs.GetService(typeof(BuildController));
         if (!string.IsNullOrEmpty(this.tfsDetails.Project))
         {
            BuildStore bs = (BuildStore)tfs.GetService(typeof(BuildStore));
            BuildData[] builds = bs.GetListOfBuilds(this.tfsDetails.Project, this.tfsDetails.BuildType);
            foreach (string buildSelected in this.chkListOfBuilds.CheckedItems)
            {
               foreach (BuildData build in builds)
               {
                  if (buildSelected.Contains(build.BuildNumber))
                  {
                     string failure = string.Empty;

                     try
                     {
                        controller.DeleteBuild(build.BuildUri, out failure);
                     }
                     catch (Exception e)
                     {
                        //MessageBox.Show( string.Format("Build delete failed for {0} : {1}\n\n", build.BuildNumber, e.Message));
                     }

Any easy way to populate grab the list of team build types is:

 Project p = (Project)this.cmbProjects.SelectedItem;
         if (p != null && IsValidTfsServerUri(this.txtServer.Text))
         {
            TeamFoundationServer tfs = new TeamFoundationServer(this.txtServer.Text);
            VersionControlServer vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
            ItemSet itemSet = vcs.GetItems("$/" + p.Name + "/TeamBuildTypes", VersionSpec.Latest,
            RecursionType.OneLevel, DeletedState.NonDeleted, ItemType.Folder);
            foreach (Item item in itemSet.Items)
            {
               string buildType = Path.GetFileName(item.ServerItem);
               if (!buildType.Equals("TeamBuildTypes"))
               {
                  //can add it to a combobox list .....this.cmbBuildTypes.Items.Add(buildType);
               }
            }
            this.cmbBuildTypes.Enabled = true;           
         }

14 Comments Filed Under [ MSBuild ]