Sergio
Sergio

Reputation: 6948

Find and delete inactive tfs branches

Is there any built-in way to find and delete (tf destroy) branches of TFS project that were inactive (I mean there were no check in operations) for a long time, let's say 1 month. Either tfs tools or maybe sql script that can do it would be ok.

Upvotes: 0

Views: 864

Answers (3)

Sergio
Sergio

Reputation: 6948

Well, whole thing wasn't hard, posting code here, might help someone:

private static string _tfLocation; //location of tf.exe
private static string _tfProject;  //our team project

static void Main(string[] args)
{
    _tfLocation = ConfigurationManager.AppSettings.Get("tfLocation");
    _tfProject = ConfigurationManager.AppSettings.Get("tfProject");
    var keepAliveBranches = ConfigurationManager.AppSettings.Get("keepAliveBranches").Split(',').ToList(); //branches that we keep anyway
    var latestDate = DateTime.Now.AddMonths(-3); //we delete all branches that are older than 3 months

    var folders = ExecuteCommand(string.Format("dir /folders \"{0}\"", _tfProject));
    var branches = folders.Split('\r', '\n').ToList();

    branches = branches.Where(b => !string.IsNullOrEmpty(b) && b.StartsWith("$")).Select(b => b.Remove(0, 1)).Skip(1).ToList();
    branches.ForEach(b => b = b.Remove(0, 1));

    foreach (var branch in branches)
    {
        if (keepAliveBranches.Contains(branch))
            continue;

        //get latest changeset
        var lastChangeset = ExecuteCommand(string.Format("history \"{0}/{1}\" /recursive /stopafter:1 /format:brief /sort:descending /noprompt", _tfProject, branch));
        var changesetDate = DateTime.Parse(Regex.Match(lastChangeset, @"\d{2}\.\d{2}\.\d{4}").Value); //get it's date 
        if (changesetDate < latestDate)                          
            //destroy
            ExecuteCommand(string.Format("destroy \"{0}/{1}\" /recursive /stopafter:1 /startcleanup /noprompt /silent", _tfProject, branch));            
    }
}

//execute console command and get results
private static string ExecuteCommand(string command)
{
    var process = new Process()
    {
        StartInfo = new ProcessStartInfo(_tfLocation)
        {
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true,
            Arguments = command
        }, 
    };
    process.Start();
    var result = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

    return result;
}

Upvotes: 0

Mike Beeler
Mike Beeler

Reputation: 4101

Unused branches contain element modification history, rather than deleting them writelock the branch and leave it. The space that is recovered is not significant.

Upvotes: 1

Bogdan Gavril MSFT
Bogdan Gavril MSFT

Reputation: 21498

You can do it but you need to write a small program that uses the TFS API to check each branch and delete the unused ones.

You can use a simple C# console app and I can tell you from experience that the TFS public API is quite intuitive and easy to use. You can get started with it here.

Here's how to display all the branches.

Upvotes: 2

Related Questions