Cancel all Suspended workflows using PowerShell in SharePoint 2013/2016

This SharePoint workflow tutorial, we will discuss how to cancel all suspended workflows using PowerShell in SharePoint 2013 or SharePoint 2016. Here, we will see how to cancel all workflows having status not equal to “Completed”.

Suppose you have a list that contains more than 1000 items and you realize the workflow which is attached to the list going to Suspended state or in progress state.

Here if you want to cancel the workflows which are not completed, then it can not be a manual task when your list is larger. If you have 10/20 items then you can do manually, but in another case, the manual process is not a good option.

I got a very good PowerShell approach from Raymun Macaulay’s Dev Blog to cancel workflows. Thanks to the author for sharing this.

Cancel all Suspended workflows using PowerShell in SharePoint

You can run, debug and test the PowerShell script using Visual studio code or using PowerShell ISE.

Below is the PowerShell script to cancel all suspended workflows in SharePoint.

#Your Shaeproint Site URL
$web = Get-SPWeb "http://yoursharepointserver.com/yoursubsite";
$web.AllowUnsafeUpdates = $true;
#Your List Name
$list = $web.Lists["YourListName"];
$count = 0
#Loop through all Items in List then loop through all Workflows on each List Items.
foreach ($listItem in $list.Items)
{
foreach ($workflow in $listItem.Workflows)
{
#Disregard Completed Workflows
if(($listItem.Workflows | where {$_.InternalState -ne "Completed"}) -ne $null)
{
#Cancel Workflows
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow);
write-output "Workflow cancelled for : " $listItem.Title;
}
}
}
$web.Dispose();

You may like following SharePoint tutorials:

This SharePoint tutorial we learned how to cancel all Suspended workflows using PowerShell in SharePoint 2013 or SharePoint 2016.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>