List Workspaces with no outstanding pending changes

This question originally came up as a StackOverflow Question. Basically, the problem was, how can I tell which of my workspaces are now unused. If you work in a single workspace then this may not be an issue; however, if you have an automated tool that interacts with TFS, you may find it’s constantly creating new workspaces and not cleaning up after itself.

The code for a simple console app is below:

    class Program
    {
        static private TfsTeamProjectCollection _tfs;

        static void Main( string[] args)
        {
            _tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("https://myUri.com" ));

            var service = _tfs.GetService< VersionControlServer>();
            Workspace[] ws = service.QueryWorkspaces( null, null, null);

            foreach( Workspace s in ws)
            {
                var pend = s.GetPendingChanges();
                if (pend.Count() == 0)
                {
                    Console.WriteLine( "Workspace {0} has no pending changes" , s.Name);
                    // s.Delete()
                    continue;
                }
            }

        }
    }

Obviously change the URI. I’ve commented out the Delete() command, and I strongly recommend that you check what it intends to delete before you let it go.

My intention is to make this into a Codeplex project at some point, and I’ll update this post if I do so. Please feel free to make recommendations for other TFS functions that you might like to see in such a project.

UPDATE:

I did manage to create that project; it’s here:

https://tfsutilities.codeplex.com/

Leave a Reply

Your email address will not be published. Required fields are marked *