How to cancel all workflows having Suspended state in SharePoint 2013?

In this post, we will discuss how to cancel all workflows having status not equal to “Completed”. Suppose you have a list which 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.

Also, check out:
Move document from one SharePoint site to another SharePoint site location in SharePoint 2010


SharePoint 2013 Add item to list using JavaScript client object model

Display SharePoint Blogs posts from Blog site on a SharePoint Online 2013 Homepage

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

Below is the PowerShell script:
#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();

Check out Best Alternative to InfoPath -> Try Now

EnjoySharePoint YouTube Channel

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →