Cancel Workflows in SharePoint using PowerShell
Requirement: There was a large list with workflow attached to it. The SharePoint Development team came with a requirement to cancel multiple workflows running on these list items, nearly 2000!
While canceling workflows on an individual item is pretty straightforward, How about canceling workflows on 1000s of items? It would be a daunting task, isn’t it?
Terminate Workflow in SharePoint using PowerShell:
Well, PowerShell can help to cancel workflows in SharePoint. If you ever have to cancel multiple running workflows on all list items, use this PowerShell script:
Cancel all workflows on a list:
$web = Get-SPWeb "https://your-sharepoint-site-url"
#List Name
$list = $web.Lists["Your-List-Name"]
# Iterate through all Items and all Workflows on Items
foreach ($item in $list.Items)
{
foreach ($wf in $item.Workflows)
{
#Cancel Workflows
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf)
}
}
To cancel all errored workflows, use the condition as:
foreach ($item in $list.Items)
{
foreach ($wf in $item.Workflows)
{
if($wf.InternalState -match 'Error')
{
#Cancel Workflows
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
}
}
}
Cancel Workflow in SharePoint
Let’s target a particular workflow:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web = Get-SPWeb "https://your-sharepoint-site-url"
#Get the List
$list = $web.Lists["Your-List-Name"]
#Get the specific workflow, Associated with the list
$WorkFlowToCancel = "Approval Workflow"
# Iterate through all Items and all Workflows on Items
foreach ($item in $list.Items)
{
foreach ($wf in $item.Workflows)
{
#Check for the particular workflow
if( ($wf.ParentAssociation.Name -eq $WorkFlowToCancel) -and ($wf.IsCompleted -ne $true) -and($wf.StatusText -ne "Canceled" ))
{
write-host "Previous workflow status:" $wf.InternalState
#Cancel Workflow
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf)
write-host "Workflow Cancelled at $($list.title)! "
}
}
}
Related Post: How to Start a SharePoint Workflow using PowerShell
Hi Salaudeen,
I’d appreciate too much if you can help me to cancel massively all workflow instances that are running (Workflow in progress), but these are crashed/corrupted, thery cannot be stopped, even after the list items deleted. I already attempted the following:
* Remove, Block, or Restore a Workflow through Workflow settings for the SharePoint list
* Powershell scripts to loop each list items and cancel them (however list items do not exists anymore, I deleted all of them)
* Powershell scripts to get the workflow associations of the SharePoint list, but it does not find any association
* I have published the Workflow again but the result is the same
Thanks in advance for suggestions about what more to do to fix my issue.
Currenty I have more than 30.000 workflow instances running and this is causing issues when end users try to edit a list items.
Hello
Please advise what the script would be when wanting to cancel any ERRORED workflow instances done by a SITE WORKLOW.
Why are you passing html code to your Get-SPWeb call? Wouldn’t this result in an error? Or is this something that your website is doing automatically to urls?
Thanks!