Best way to handle/archive Workflow History List

Msdc 271 Reputation points
2020-10-14T14:48:13.583+00:00

Hello,

I am running into issues with a custom SharePoint list where the workflow seems to be erroring but I am unable to access the workflow history list to see what has not run properly for each item. My list view threshold is set to 15,000 but the workflow history list is at 25,000. I am leaning toward just totally wiping out every item in the history list but I know that means i lose history on all workflow history for each item submitted which I dont want. Is there anything possible for me to do besides using powershell to totally empty out the list. Of course I could increase threshold to 30,000 but don't want that. I just hate not being able to see anything in the workflow history list to see what has completed or errored out.

Thanks for any help

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,206 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,569 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,794 questions
0 comments No comments
{count} votes

Accepted answer
  1. Emily Du-MSFT 41,691 Reputation points Microsoft Vendor
    2020-10-15T06:47:10.193+00:00

    @Dez-4361
    The answer provided by sharatha can be used as a workaround.

    Based on your description, I understand that you want to purge Workflow History list. Here’s PowerShell for you.

    Note: The $cutoffDate determines which items to keep. As configured, the cutoff date will be current date - 1days. Any items older than 1days will be removed. You can set $cutoffDate according to your requirement.

    $webURL = "your site collection URL"  
    $cutoffDate = (Get-Date).AddDays(-1)  
       
    Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue  
       
    Write-Host "`n- Opening web site $webURL"  
    $web = Get-SPWeb $webURL -ErrorAction SilentlyContinue  
    if (!($web)) {  
      Write-Host -ForegroundColor Red "- Unable to open web site $webURL"  
      Exit  
    }  
       
    Write-Host "- Opening workflow history list"  
    $list = $web.lists["Workflow History"]  
    if (!($list)) {  
      Write-Host -ForegroundColor Red "- Unable to open Workflow History list"  
      $web.Dispose()  
      Exit  
    }  
       
    DO {  
      $cutoffDateF = "{0:MM/dd/yyyy}" -f $cutoffDate  
      Write-Host "- Looking for items where 'Date Occurred' <= $cutoffDateF"  
      Write-Host  
      $rowlimit = 100  
      $caml="<OrderBy><FieldRef Name='ID' Ascending='TRUE' /></OrderBy><OrderBy><FieldRef Name='ID' /></OrderBy>"  
      $camlQuery = New-Object Microsoft.SharePoint.SPQuery  
      $camlQuery.RowLimit = $rowLimit  
      $camlQuery.Query = $caml  
      $items = $list.GetItems($camlQuery)  
       
      If (!($items)) {  
        Write-Host -ForegroundColor Red "- No items to process"  
        Break  
      }  
       
      $itemTotal = $items.Count  
      $itemCurrent = 1  
      $itemsDeleted = 0  
      Write-Host "- Items to process: $($itemTotal)"  
      $timeStarted = Get-Date  
      For ($i=$items.count-1; $i -ge 0; $i--) {  
        $item = $items[$i]  
        $itemCurrentF = $itemCurrent.ToString().PadLeft($itemTotal.ToString().Length,"0")  
        $dateOccurredF = "{0:MM/dd/yyyy}" -f $item["Date Occurred"]  
        Write-Host "- Processing item $itemCurrentF of $($itemTotal) ... " -NoNewline  
        If ($item['Date Occurred'] -lt $cutoffDate) {  
          $item.Delete()  
          Write-Host "Deleted - $dateOccurredF <= $cutoffDateF"  
          $itemsDeleted++  
        }  
        Else {  
          Write-Host "Ignored - $dateOccurredF => $cutoffDateF"  
        }  
        $itemCurrent++  
      }  
       
      $timeFinished = Get-Date  
      $timeDuration = ("{0:hh\:mm\:ss}" -f ($timeFinished - $timeStarted))  
      If ($timeDuration.Length -gt 8) { $timeDuration = $timeDuration.Substring(0,12) }  
      Else { $timeDuration += ".000" }  
      $timeTotalSeconds = [Math]::Round(($timeFinished - $timeStarted).TotalSeconds)  
      $deletedPerSecond = [Math]::Round($itemsDeleted / $timeTotalSeconds)  
      Write-Host  
      Write-Host "Batch Started:  $timeStarted"  
      Write-Host "Batch Finished: $timeFinished"  
      Write-Host "Batch Duration: $timeDuration"  
      Write-Host  
      Write-Host "Total Seconds:  $timeTotalSeconds"  
      Write-Host "Items Deleted:  $itemsDeleted"  
      Write-Host "Deleted/Second: $deletedPerSecond"  
      Write-Host  
    } UNTIL ($itemsDeleted -le 0)  
       
    $web.dispose()  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Sharath Kumar Aluri 3,071 Reputation points
    2020-10-14T15:01:08.66+00:00

    Just change the Workflow History List to a New History List for the Existing workflow List, in that way All the currently running instance will use the Workflow History List and newly created instances will goes to New History List which is going to be your New Workflow History List. you can do this via SharePoint Designer, below article will help you to create a workflow history list.

    https://www.spguides.com/sharepoint-online-workflow-history-list/

    Thanks & Regards,