tech·nic·al·ly agile

How to delete work items from TFS or VSO

Learn how to efficiently delete work items from TFS or VSO with expert tips and code examples. Streamline your project management today!

Published on
3 minute read
Image
https://nkdagility.com/resources/MYXrtTYV2UD

Have you ever created a bunch of work items that you decided later that you had to delete. Well I have… especially as a user of the TFS Integration Platform. And when things go wrong there they can really go wrong.

Now while you can put stuff into the “removed” state it is still hanging around cluttering up the place. The only way out of the box to remove items is to give the ID for each work item that you want to delete and execute the command line for each one.:

1witadmin destroywi /collection:CollectionURL /id:id [/noprompt]

WARNING: This code can result in total loss of all work items you have if you miss key a query! Be careful… and you are on your own. Don’t blame me, and no… I can’t get them back for you…

Well that’s just great unless you have a couple of thousand things to delete. So I knocked up a little bit of code to do it for me. Now, since I have had to knock it up a bunch of times before I thought that I had better share it. I started this blog in the first place so that I would remember things.

 1using Microsoft.TeamFoundation.Client;
 2using Microsoft.TeamFoundation.WorkItemTracking.Client;
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6using System.Text;
 7using System.Threading.Tasks;
 8 
 9namespace ConsoleApplication1
10{
11    class Program
12    {
13        static void Main(string[] args)
14        {
15 
16            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("http://tfs.company.com:8080/tfs/DefultCollection"));
17            WorkItemStore store = tpc.GetService();
18            string query = @"SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = 'projectName'  AND  [System.AreaPath] UNDER 'projectName\_TOBEDELETED' ORDER BY [System.Id]";
19            WorkItemCollection wis = store.Query(query);
20            IEnumerable x = from WorkItem wi in wis select wi.Id;
21            Console.WriteLine(string.Format("DESTROY {0} work items (they really can't be resurrected): y/n?", wis.Count));
22            ConsoleKeyInfo cki = Console.ReadKey();
23            Console.WriteLine();
24          if (cki.Key.ToString().ToLower() == "y")
25            {
26            try
27                {
28                    Console.WriteLine("Deleting....");
29                    IEnumerable y = store.DestroyWorkItems(x.ToArray());
30                    Console.WriteLine("DONE");
31                    foreach (var item in y)
32                    {
33                        Console.WriteLine(item.ToString());
34                    }
35                }
36                catch (Exception)
37                {
38 
39                    Console.WriteLine("Things have gotten all pooped up please try again!");
40                }
41        
42            }
43 
44          Console.WriteLine("Freedom");
45        }
46   
47    }
48}

The first thing that you may notice is that I search for items in a specific area path. I use _TOBEDELETED as it is obvious what is going to happen to things that end up there. Although I did work with a user who complained that all his files had gone missing. When asked where he kept them he pointed at the recycle bin on his desktop!

Anyhoo… just in case you made a mistake it will let you know how many work items that you are deleting. It’s a simple check but I have had it say “100,000” work items… AS you can imagine I very carefully terminated the program (never trust the ’no’ option).

Comments

Related blog posts

Related videos

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

Lockheed Martin Logo
Boxit Document Solutions Logo
ProgramUtvikling Logo
Brandes Investment Partners L.P. Logo

NIT A/S

Genus Breeding Ltd Logo
Capita Secure Information Solutions Ltd Logo
Teleplan Logo
Schlumberger Logo
Microsoft Logo
Higher Education Statistics Agency Logo
Boeing Logo
DFDS Logo
Deliotte Logo
Bistech Logo
New Signature Logo
Emerson Process Management Logo
Qualco Logo
Ghana Police Service Logo
Royal Air Force Logo
Nottingham County Council Logo
Washington Department of Enterprise Services Logo
Washington Department of Transport Logo
New Hampshire Supreme Court Logo
Hubtel Ghana Logo
Schlumberger Logo
Teleplan Logo
MacDonald Humfrey (Automation) Ltd. Logo
YearUp.org Logo
Deliotte Logo