Vadim Tabakman

WARNING

DO NOT run this on a production system unless absolutely necessary.  It iterates through all sites, lists, items and workflows.  I have not tested this for performance and system impact.

There have been some requests on how to cancel all running workflows. I've looked online and have found a number of C# and PowerShell scripts, but each has resulted in issues of one sort or another.

So I've created the script below.  It's not pretty and probably can be optomized, but it works and I'm happy with it.

What it does, is iterate through site, through each list in the site, each item in the list and then each workflow and if it's not "Completed" and not "Cancelled", then it cancels the workflow.

I have found that if you cancel a workflow that is completed or already cancelled, you'll find a mesasge in the workflow history that the workflow has been cancelled, even though it wasn't running.  Also, if it's a Nintex Workflow, you'll then end up getting an email telling you the workflow was cancelled, when you shouldn't have even tried to cancel it.

So the below script works for list and site workflows.

Why not use the online scripts?

There are 2 issues I had with scripts I found online.

  1. Scripts were iterating through each web using a ForEach, and that would crash the script since it affects the foreach collection of webs.
  2. Scripts were cancelling workflows that weren't running.

Improvements that could be made

It'd be nice to turn this into a ps1 file that takes arguments.  So you can pass in the url to the parent site and possibly a listname, item index or workflowname to make this a little smarter.  But for now, this works and you can modify it as you see fit.

Below is the PowerShell Script

It's hardcoded to localhost but you can change it your own site, or a different url.

$site = Get-SPSite "http://localhost"
if($site)
{
  $spWebCollection = $site.AllWebs;
  $i = 0;
  $iWebCount = $spWebCollection.Count;

  do
  {
    Write-Host $i;

    $web = $spWebCollection[$i];
    $spListCollection = $web.Lists;
    $iListCount = $spListCollection.Count;

    $j = 0;
    do
    {
      $list = $spListCollection[$j];
      $spItemCollection = $list.Items;
      $iItemCount = $spItemCollection.Count;
      $k = 0;

      do
      {
        $item = $spItemCollection[$k];
        $spWorkflowCollection = $item.Workflows;
        if($spWorkflowCollection)
        {
          $iWorkflowCount = $spWorkflowCollection.Count;
          $l = 0;
 
          do
          {
            $workflow = $spWorkflowCollection[$l];
            if($workflow)
            {
              if($workflow.InternalState -ne 'Completed' -and $workflow.InternalState -ne 'Cancelled')
              {
                [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow);
              }
            }
            $l++;
          }
          while ($l -lt $iWorkflowCount)
        }
        $k++;
      }
      while ($k -lt $iItemCount)
      $j++;
    }
    while ($j -lt $iListCount)
   
    # Site Workflows
    $spWorkflowCollection = $web.Workflows;
    if($spWorkflowCollection)
    {
      $iWorkflowCount = $spWorkflowCollection.Count;
      $w = 0;

      do
      {
        $workflow = $spWorkflowCollection[$w];
        if($workflow)
        {
          if($workflow.InternalState -ne 'Completed' -and $workflow.InternalState -ne 'Cancelled')
          {
            [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow);
          }
        }
        $w++;
      }
      while ($w -lt $iWorkflowCount)
    }
    $web.Dispose();
    $i++;
  }
  while ($i -lt $iWebCount)

  $site.Dispose()
}

Posted by Vadim Tabakman Thursday, May 10, 2012 2:11:00 PM Categories: Nintex Workflow PowerShell SharePoint
Copyright Vadim Tabakman
Rate this Content 0 Votes

Comments

Friday, April 19, 2013 2:29:04 PM

re: PowerShell - Cancel all Running Workflows

How come everytime I'm Googling something, I find the best answer on your blog? Because you have all the answers, of course! :P

Thanks as usual my friend, this is just what we are looking for!

Friday, April 19, 2013 2:30:36 PM

re: PowerShell - Cancel all Running Workflows

Hahah Thanks Josh.  I really appreciate that!!!

Let me know if there's something you think would interesting to post about.

Tuesday, April 30, 2013 12:00:24 PM
Randy Hinders

re: PowerShell - Cancel all Running Workflows

Vadim,

As always beautiful post!

But I have noticed a bug with SharePoint.... If a workflow has been deleted (under workflow settings not Nintex) and an item still has that deleted workflow running, then this script will not cancel the workflow.  It remains running in an orphaned state.

What do you think about this snafu?

Randy

Tuesday, April 30, 2013 12:03:33 PM

re: PowerShell - Cancel all Running Workflows

Hi Randy,

It's possible the workflow isn't actually running, but the NW database is out of sync with SharePoint in this case.

You should be able to resolve that by running :

nwadmin -o SyncTerminatedWorkflow -url "siteurl"

If you put a -preScan on the end of that command line, it'll tell how many items it has found out of sync. Then rerun it with out the -preScan and it should fix it.

cheers,

Vadim

Tuesday, April 30, 2013 12:41:32 PM
Randy Hinders

re: PowerShell - Cancel all Running Workflows

You are correct (as always).. that worked :)

Thanks again,

Randy

Tuesday, April 30, 2013 12:43:51 PM

re: PowerShell - Cancel all Running Workflows

Awesome.  Glad to be of service :)

Friday, September 26, 2014 11:48:59 AM

Andres - That's an e

Andres - That's an excellent idea. My first tghuoht was to create a SharePoint list to store the information you want to be in the email. This list will only need one record and it will need a column that is of type Multiple Lines of Text. Add a step to the workflow to create a dynamic string that contains the information for each task that you want to show up in the email. Then create a new dynamic string that extracts the information currently in that Multi-Line Text field you created, apends the line you created just before that, then update that same Multi-line text field with the new combined string you just created.That's how you can compile everything for the email using this looping method. The problem comes in that this workflow dies when it finishes so it can't send all that information out in an email.It's another hack, but maybe you could also write another workflow that runs on the list that has tha Multi-line text field. It would run whenever an item is updated. The workflow could pause for 15-minutes at the very beginning, or maybe even much less--you'll need to play with this to see what works, it depends on how frequently the looper workflow runs. Anyway, it can compare the value in the multi-line text field and if it has been updated since the workflow began then it dies. If it hasn't been updated in 15 minutes it sends out the email you want and clears the text that is in the multi-line text field.Great idea! Good luck!
Friday, November 7, 2014 3:13:20 AM
Mugilarasu
Gravatar

re: PowerShell - Cancel all Running Workflows

Hi Randy,

 

We have an unique issue, that we need to cancel and restart only the in progress workflow. We are using Nintex workflow, We need to restart only the in progress workflow. is there any specific internal state for In progress status. i tried with ($workflow.InternalState -like 'In progress') . is this correct , if wrong can you please help me out. 

Wednesday, April 15, 2015 8:23:44 AM
Juli
Gravatar

re: PowerShell - Cancel all Running Workflows

Mugilarasu - Workflow status is identified by SharePoint as a number.   Rather than using 'In Progress' you will probably have to use the number '2'.

Vadim, is this only to cancel all Nintex workflows? or will it close spd workflows also?

 

J

Wednesday, April 15, 2015 8:52:19 AM

re: PowerShell - Cancel all Running Workflows

Hi Juli,

it will cancel SPD workflows also :)

cheers,

Vadim

Tuesday, May 12, 2015 12:40:20 PM
Juli
Gravatar

re: PowerShell - Cancel all Running Workflows

One additional question.    When all these workflows are terminated will they send out the normal email indicating they have been terminated and if so is there any way to suppress that?

 

Tuesday, May 12, 2015 1:24:47 PM

re: PowerShell - Cancel all Running Workflows

Hi Juli,

 

yes it will.  To stop that, go to your Site Settings  and in the Nintex Workflow links, there should be one for managing error notifications.  You can turn it off there.

cheers,

Vadim

Friday, May 15, 2015 7:57:52 AM
Juli
Gravatar

re: PowerShell - Cancel all Running Workflows

Any way to set that in the script itself?   If I want to run this on localhost, that's a whole lot of sites to have to edit first.

 

Friday, May 15, 2015 8:23:10 AM

re: PowerShell - Cancel all Running Workflows

Hi Juli,

I believe that that setting is in the Nintex Workflow data, in the ErrorNotifications table. But I have no idea how to add entries to that from the PS script.

From a coding standpoint, I think it's definitely possible, but I've never done that.

cheers,

Vadim

Tuesday, May 26, 2015 12:35:53 PM
Juli
Gravatar

re: PowerShell - Cancel all Running Workflows

One last question.   If we turn off smtp and then run this.  Will all the notification emails queue up and then when smtp is turned back on go out en masse or will this prevent them from being sent at all?

 

 

Tuesday, May 26, 2015 12:45:16 PM

re: PowerShell - Cancel all Running Workflows

Hi Juli,

I don't believe they do.  Nintex doesn't queue up emails like that.

cheers,

Vadim

 

Monday, August 3, 2015 8:38:03 AM
Juli
Gravatar

re: PowerShell - Cancel all Running Workflows

So I just ran this in my dev envirnoment and here is what I discovered.    An email was sent to the initiator of each workflow and to anyone assigned to open tasks that the workflow had been cancelled.

Do you know of anyway to prevent that?

 

 

Monday, August 3, 2015 12:39:30 PM

re: PowerShell - Cancel all Running Workflows

Hi Juli,

go into Site Settings and configure the Error and Cancelled Notifications settings.

cheers,

Vadim

Tuesday, October 20, 2015 7:04:54 PM
Jarrad
Gravatar

re: PowerShell - Cancel all Running Workflows

Hey Vadim,

I just used this after moving doing a Prod to UAT refresh.

One thing to note is that you have to use an account that has access to the relevant Sharepoint databases.

Thanks again for a very useful script.

Jarrad

Monday, April 18, 2016 11:40:33 AM
New Admin
Gravatar

re: PowerShell - Cancel all Running Workflows

Hey Vadim,

 

Do you have script to Cancel or delete all workflows running on any site collection or sub site for SharePoint Online .

 

Thanks

New Admin

Monday, April 18, 2016 11:54:56 AM

re: PowerShell - Cancel all Running Workflows

I'm afraid I don't have an scripts for O365 at this stage.

Vadim

Tuesday, September 26, 2017 9:11:37 AM

re: PowerShell - Cancel all Running Workflows

Great article, as usual, Vadim.  However, when I apply this I find that the number of workflows (in any given workflow state) that this script finds for a given SharePoint site is far less than what the workflow report web part finds.  (As a pre-test, to gather information, I have modified the script to just report the workflow states, not cancel running workflows and have commented out the if clause that filters out completed and canceled workflows.) In one case the web part report (Nintex Workflow Report Viewer wp) shows a total of  3317 "Completed" workflows while your (modified) script  finds only one tenth as many.  Ditto with workflows in other states.  

Can you offer some insight as to why the web part  consistently reports many more workflows in each workflow state compared to those found in the powershell collections?

Thanks,

Bruce

You must sign in to this site to post comments.
Already Registered?
Sign In
Not Yet Registered?
Register

Statistics

  • Entries (290)
  • Comments (1778)

Categories