Cleanup Workflow History

A command-line tool to cleanup the workflow history list in a SharePoint site. SharePoint has a "Workflow Auto Cleanup" job that deletes the association, but it does not delete items from the Workflow History list. Background When I started my job, I was told that one of th

C# (979.6 KB)
4.3 Star
7,889 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • Has this been tested on SP2013?
    3 Posts | Last post July 01, 2016
    • I would like to use this in sp2013, do you know of any problems or codes changes?
      Thank you for your help in advance.
    • Anyone tried this in SP 2013?
    • The source code needs to be recompiled for SharePoint 2013 and up.  Download Visual Studio 2012 - use in trial mode - - then open the project.  You'll need to resolve references to SharePoint.  I simply copied the Microsoft.SharePoint.dll from my SharePoint server C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI to the project folder.  Deleted and re-added the reference to this DLL.  Change the project Application Target Framework to .NET Framework 4.5 - then Build | Build Solution.  
      You'll have a working executable for SharePoint 2013 at this point. 
  • I can not get it to run. Just keep getting instructions / help popup
    2 Posts | Last post October 29, 2015
    • Thanks for writing this. I am having an issue I was hoping you could assist me with. First which WorkflowHistoryCleanup.exe should I be running? There are several (\bin\Release; \obj\release; \bin\debug; \obj\debug).
      Everytime I attempt to run this, it just pops up with the same help information and instructions, no errors, no log. Here is what I am running (from \bin\release):
      WorkFlowHistoryCleanup.exe http://omdc-share3:28933 "Workflow History" 90 100 0 false false c:\cleanupworkflow.log
      After I run that, it just pops up with :
      CleanupWorkflowHistory.exe SiteUrl ListName NumDays MaxHistoryListDelete Permane
      ntDelete ReportOnly
      SiteUrl - URL to the SharePoint site
      ListName - Name of the Workflow History list
      NumDays - Delete items with Modified date < [Today - NumDays])
      MaxHistoryListDelete - Maximum number to delete from Workflow History list
      MaxRecycleBinDelete - Maximum number to delete from Recycle Bin
      PermanentDelete - Permanently delete the list items from the Recycle Bin
      ReportOnly - Run in read-only mode
      LogFileName - Name of the file to use for logging (overwrite if it exists)
      CleanupWorkflowHistory.exe http://moss/dematic_tools/TimTest 'Workflow History'
      60 100 100 false true Cleanup.log
      I have been trying to get this working for 2 days. Any help is appreciated. Again, thanks for writing this and making it available.
    • I figured it out. I was using "C:\LOGFILENAME.log". After taking out "C:\" from the log file name, it ran great. Thanks again for writing and sharing.
  • SharePoint 2010 version
    1 Posts | Last post July 25, 2014
    • The only download available appears to be for Sharepoint 2007. Can someone post a SharePoint 2010 version?
  • When you Build new version for SharePoint 2010 ? and also any power shell command do you have planed ?
    1 Posts | Last post May 31, 2013
    • When you Build new version for SharePoint 2010 ? and also any power shell command do you have planed  for this . 
  • Another Thank You
    1 Posts | Last post February 26, 2013
    • Started to write this myself when I stumbled across this in a search.  Works very nicely.
  • Thank you!
    2 Posts | Last post October 20, 2012
    • Not a question, but I didn't see any method for contacting the author of this app. Thank you very much! This was exactly what we needed and worked beautifully to clean a massive workflow history list in our environment.
    • WesleyTech - thanks!  Glad you found it helpful.
  • Complete Novice Question
    2 Posts | Last post September 04, 2012
    • Tim - 
      This looks to be exactly what I'm looking for to remove the more than 3 million + items off more than one workflow history list. However, being a complete novice, I thought that the code was running, but it's in "read only" mode - is there a way I can change this? Also, do I need to do anything with the executable in VS? Thanks for your help!
    • This is the last switch before the log file name - "ReportOnly – Run in read-only mode" - setting this to FALSE should permanently remove the records.
  • Recycle Bin - Deletion Issue
    1 Posts | Last post September 04, 2012
       We're also getting the following error that was previously reported:
      20/02/2012 2:15:13 AM:  ERROR in PurgeSiteRecycl​eBin: Operation is not valid due to the current state of the object.
       Any ideas on how to resolve this? The task was run with the farm setup account.
  • does it check the workflow status
    2 Posts | Last post June 18, 2012
    • Hi Tim,
      Before deleting item from Workflow History does this code check if the workflow is completed?
      In my case there are some workflows that are running for more than 2 months. They have been idle for some reason. I don't want to delete any history related to such items, and only delete the history for items who's workflow was completed / cancelled atleast 30 days back.
      Is that possible? If yes, what would be the code change?
    • Hi Vivek,
      No, it does not check the state of the workflow.  I'm sure it could be added, but I'm not sure what the code changes would be.
  • Recycle Bin
    4 Posts | Last post May 04, 2012
    • Hi Tim,
      Do you have any clue on why the script is not deleting items from the 2nd stage recycle bin?
      this is what I'm running:
      CleanupWorkflowHistory.exe http://site/subsite "Workflow History" 60 5 5 true false test.log
      This is the result:
      SiteUrl: http://site/subsite
      ListName: Workflow History
      NumDays: 60
      MaxHistoryListDelete: 5
      MaxRecycleBinDelete: 5
      PermanentDelete: True
      ReportOnly: False
      LogFileName: test.log
      4/4/2011 12:25:44 PM: started CleanupWorkflowHistory
      4/4/2011 12:25:59 PM:  started processing batch.
      4/4/2011 12:26:00 PM:   MaxHistoryListDelete: 5
      4/4/2011 12:26:00 PM:   5 items deleted
      4/4/2011 12:26:00 PM:  finished processing batch
      4/4/2011 12:26:00 PM:  started purging Web Recycle Bin
      4/4/2011 12:26:00 PM:   5 items in Web Recycle Bin
      4/4/2011 12:26:00 PM:   MaxRecycleBinDelete: 5
      4/4/2011 12:26:00 PM:   0 items purged
      4/4/2011 12:26:00 PM:  finished purging Web Recycle Bin
      4/4/2011 12:26:00 PM:  started purging Site Recycle Bin
      4/4/2011 12:26:00 PM:   1704296 items in Site Recycle Bin
      4/4/2011 12:47:23 PM:   MaxRecycleBinDelete: 5
      4/4/2011 12:47:23 PM:   0 items purged
      4/4/2011 12:47:23 PM:  finished purging Site Recycle Bin
      4/4/2011 12:47:24 PM: finished CleanupWorkflowHistory
      I'm tryng to figure out why items are not being deleted from the recycle bin :(
    • I also came across the same scenario and learned that when we use SPRecycleBinItem.Delete() on first stage recycle bin, item gets permanently deleted. It wont be going to the second stage recycle bin and there for does not require a cleanup of 2nd stage recycle bin.
      Also one more point I would like to add is that we have to specify which recycle bin to query by setting  
      query.ItemState = SPRecycleBinItemState.FirstStageRecycleBin;
      otherwise we cannot make sure that we querying the correct recycle bin.
      Please correct me if I am wrong
    • Charls & Luis - I haven't ran into a situation where workflow history items weren't deleting from the 2nd stage recycle bin.  This tool will only delete items in recycle bins that are from the Workflow History list you provide in the parameter - that is what this line determines:
      if (item.DirName.Contains(_ListName)) // item is from the Workflow History list 
      To be honest, I don't remember the reason why I check both recycle bins.  I'm sure there was a reason, but it has been a while since I worked on this project.
    • In my case input list name did not match the letter case returned by item.DirName. first letter was capital.
      I changed : if (item.DirName.Contains(_ListName))
      to: if(item.DirName.ToLower().Contains((_ListName.ToLower())) )
1 - 10 of 17 Items