SharePoint 2010: How to Delete Completed Workflow Instances

SharePoint 2010: How to Delete Completed Workflow Instances



Introduction:

Few days back a contributor requested me to write a tool that will cleanup completed workflow instances from a list. Hence I have developed this script, it takes site url and list name as input and then it spans through all items in the list and clears up all completed workflow instances.

This works for sharepoint 2007 as well as sharepoint 2010. This can be executed from any WFE in the farm. The code is as below:

C# Code Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
 
 
namespace CompletedWorkflowCleanUp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the site url");
            String siteUrl = Console.ReadLine();
            Console.WriteLine("Please enter the list title");
            String listName = Console.ReadLine();
            using (SPSite site = new SPSite(siteUrl))
            {
                SPWorkflowManager workflowManager = site.WorkflowManager;
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists[listName];
                    SPListItemCollection listItems = list.Items;
                    foreach (SPListItem listItem in listItems)
                    {
                        SPWorkflowCollection wCollection = listItem.Workflows;
                  
                        for (int i = 0; i < wCollection.Count; i++)
                        {
                            if (wCollection[i].IsCompleted)
                            {
                                workflowManager.RemoveWorkflowFromListItem(wCollection[i]);
                                listItem.Update();
                            }
 
 
                        }
                       
                    }
                    Console.WriteLine("Completed workflow instances are deleted for the list" +list);
                    Console.ReadLine();
                }
            }
        }
    }
}
Leave a Comment
  • Please add 4 and 5 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Gokan Ozcifci edited Revision 6. Comment: title and formatting  

  • Patris_70 edited Revision 5. Comment: deleted (en-US) from title

  • Craig Lussier edited Revision 4. Comment: added en-US to tags and title

  • Ed Price - MSFT edited Revision 3. Comment: Title casing

  • Craig Lussier edited Revision 2. Comment: minor text edit

  • Craig Lussier edited Revision 1. Comment: added toc

  • Ed Price - MSFT edited Original. Comment: Adding tags

Page 1 of 1 (7 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Ed Price - MSFT edited Original. Comment: Adding tags

  • Craig Lussier edited Revision 1. Comment: added toc

  • Craig Lussier edited Revision 2. Comment: minor text edit

  • Ed Price - MSFT edited Revision 3. Comment: Title casing

  • Craig Lussier edited Revision 4. Comment: added en-US to tags and title

  • Patris_70 edited Revision 5. Comment: deleted (en-US) from title

  • Gokan Ozcifci edited Revision 6. Comment: title and formatting  

Page 1 of 1 (7 items)