Removing old workflow instances with Powershell

As most of you will know, there are two basic types of workflow in SharePoint. Some workflows are authored in SharePoint Designer whilst others are created within Visual Studio. This article is aimed at those who author workflows in SPD.

Each time a new workflow is published within SPD, a new instance is added. What this means is that each time a workflow is published, it overwrites a previous version. If an administrator were to check the active workflows, they’d see a history that looks like this

I don’t believe that there is any performance overhead for having these historical workflows listed but wouldn’t it be nice to have a way to remove these. This article is going to show you how.

  1. Download this PowerShell Script from the TechNet Gallery. Download it somewhere that is easy to find, such as C:/Scripts
  2. Right Click on the script and select Open
  3. Look for the following variables and change them as requird
    1. $web: Include the URL of your Site
    2. $listToCancel: Use the display name of the list / library that has the workflow versions you want to delete
    3. $wfToKeep: Use the name of the Workflow to be pruned
  4. Save and close the script
  5. Right click and select Run with PowerShell. Click Yes to the script. You’ll see this as the script has been downloaded from TechNet rather than written locally

  1. Once the script has run, return to your workflow history page. You should see only one version of the workflow as below

Caveats

Please be mindful of any workflow instances that may still be in operation. The script won’t be too choosy in what it removes.

Credits

Thanks to Matthew Yarlett for looking over this script for me.

Advertisements