Cancel multiple SharePoint Workflows using PowerShell

DaviideSnow picture DaviideSnow · Sep 29, 2011 · Viewed 21k times · Source

How is it possible to cancel all running workflows in a SharePoint (2010) List?

I found this script via technet.

http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/d3913265-9712-4e61-9e38-1f9b78c8f718/

CODE:

using (SPSite oSite = new SPSite("<your url>"))
{
    foreach (SPWeb oWeb in oSite.AllWebs)
    {
        oWeb.AllowUnsafeUpdates = true;

        // stop list workflows
        foreach (SPList list in oWeb.Lists)
        {
            foreach (SPListItem oItem in list.Items)
            {
                foreach (SPWorkflow workflow in oItem.Workflows)
                {
                    SPWorkflowManager.CancelWorkflow(workflow);
                }
            }
        }

        // stop site workflows
        foreach (SPWorkflow workflow in oWeb.Workflows)
        {
            SPWorkflowManager.CancelWorkflow(workflow);
        }

        oWeb.AllowUnsafeUpdates = false;
        oWeb.Dispose();
    }
}

Many thanks for your help.

Answer

Justin Brink picture Justin Brink · Oct 13, 2011

Give this one a shot.

I modified it so that it is done on a specific list on a specific web.

#Site URL
$web = Get-SPWeb "http://urlforsite.com";
$web.AllowUnsafeUpdates = $true;    

#List Name
$list = $web.Lists["ListName"];

# Iterate through all Items in List and all Workflows on Items.         
foreach ($item in $list.Items) {
foreach ($wf in $item.Workflows) {

#Cancel Workflows        
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);      
}
}
$web.Dispose();

Worked just fine for me. Let me know if it works for you.