Monday, December 26, 2011

Purge Workflow history list in Sharepoint

If your workflow history have large amount of data (say in millions), then purging it would be a challenge. If you try to delete the list in UI, you might get "Time out error". If you try to delete the list through Sharepoint designer or through stsadm cmdlet , it might run for hours. 
The best approach to purge the data is deleting them chunk by chunk.
You have to point the running workflow to a new history list, then we can delete the old the history list completely.
In the below sample I'm using processbatchdata for faster deletion. The below code will delete 2000 items in 3 iteration. You can increase the iteration at your will. Also you need to modify the startID accordingly after each execution.

  
private static int iteration = 3;
        private static int Count = 2000;
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("first argument: No of iteration");
                Console.WriteLine("YourprgramName.exe 5");
                Console.WriteLine("******Program terminated : Argument missing! Please give one argument ****");
             
            }
            else
            {
                try
                {
                    iteration = Convert.ToInt32(args[0]);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error : Failed to convert arguments: {0}", ex.Message);
                    
                    return; // exit
                }
                Console.WriteLine("Current System Time (Start) : " + DateTime.Now.ToString());
                try
                {
                    for (int iterate = 0; iterate < iteration; iterate++)
                    {
                        int start =1;
                        Console.WriteLine("Start Index: "+ start.ToString());
                        using (SPSite site = new SPSite("http://siteurl"))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                SPList wflist = web.Lists["Workflow History"]; //workflow history
                                string wflistID = wflist.ID.ToString();
                                Console.WriteLine("No of items before deletion: " + wflist.ItemCount);
                                Console.WriteLine("Building query... ");
                                StringBuilder batchString = new StringBuilder();
                               batchString.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch OnError=\"Continue\">");
                                int end = start + Count - 1;
                                for (int i = start; i <= end; i++)
                                {
                                    batchString.Append("<Method>");
                                    batchString.Append("<SetList Scope=\"Request\">" + wflistID + "</SetList>");
                                    batchString.Append("<SetVar Name=\"ID\">" + Convert.ToString(i) + "</SetVar>");
                                    batchString.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
                                    batchString.Append("</Method>");
                                }
                                batchString.Append("</Batch>");
                                //Console.WriteLine(batchString.ToString());
                                try
                                {
                                    web.AllowUnsafeUpdates = true;
                                    Console.WriteLine("Executing query... ");
                                    Console.WriteLine("Batch Execution (Start) : " + DateTime.Now.ToString());
                                    string result = web.ProcessBatchData(batchString.ToString());
                                    //Console.WriteLine(result);
                                    web.AllowUnsafeUpdates = false;
                                    Console.WriteLine("Batch Execution (END) : " + DateTime.Now.ToString());
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Process batch error : " + ex.Message);
                                }
                                Console.WriteLine("No of items before deletion: " + wflist.ItemCount);
                                using (SPSite site1 = new SPSite("http://siteurl/"))
                                {
                                    using (SPWeb web1 = site1.OpenWeb())
                                    {
                                        Console.WriteLine("No of item after deletion: " + web1.Lists["Workflow History"].ItemCount);
                                    }
                                }
                                Console.WriteLine("--------------------------------------------------");
                            }
                        }
                    }
                    Console.WriteLine("Current System Time (End) : " + DateTime.Now.ToString());
                    Console.WriteLine("-----------Program Completed------------");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error : " + ex.Message);
                    Console.WriteLine("******Program terminated due to error **** ");
                }
            }
        }
       
          

No comments:

Post a Comment