locked
Delete SharePoint Workflow History List Questions RRS feed

  • Question

  • I found the code below here : https://social.technet.microsoft.com/Forums/sharepoint/en-US/79aa3bd4-d334-4614-9304-15998a95aefb/delete-list-with-43-million-items-workflow-history-list?forum=sharepointadminprevious

    I originally had 81K items in the workflow history list..Dating back to a year ago, which obviously didn't need to be saved in the workflow history list. And we all know Workflows start to have issues when this WF History list get a lot of items in it.

    my questions are now that I have it cleaned up...

    I have workflows that trigger 30 days prior to when an item expires.  so for example if an item expires 12-15-15, the workflow sends an email 11-15-15.  but these expiration dates can be at any time of the year. for example lets say the item was entered on  2-2-15 with expire date of 12-15-15 -  workflow is now  triggered so that emails would be send in 11-15-15 ....

    So if I delete the workflow history list item will it affect the workflow waiting to be triggered?

    Is there a way with the code below to capture workflows that have completed, error'd out, or failed to start?

    As I figure those would be safe to delete if deleting items from the workflow history list affects workflows waiting to be triggered?

    or would it be possible for the code below to be modified  to delete based on either ID of list item and or date range?? 

    like  say  delete  completed workflows at 100 items every thirty days?

    but I can not have a current waiting workflow to trigger & history beaffected by the workflow history list delete.

    ********************************** Code **************************************************************

    $weburl = "http://"
    $listname = "Workflow History"

    #param($weburl,$listname)
    #if ($weburl -eq $null -or $listname -eq $null)
    #{
    #    write-host -foregroundcolor red "-weburl or -listname are null."
    #    return
    #}
     
    Add-PSSnapin Microsoft.SharePoint.Powershell -EA 0
    $web = get-spweb $weburl
    $list = $web.lists[$listname]
     
    $stringbuilder = new-object System.Text.StringBuilder

    try
    {
        $stringbuilder.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><ows:Batch OnError=`"Return`">") > $null
     
        $i=0
     
     $spQuery = New-Object Microsoft.SharePoint.SPQuery
        $spQuery.ViewFieldsOnly = $true
     $spQuery.RowLimit = 1 // # of items to delete
     
     $items = $list.GetItems($spQuery);
        $count = $items.Count
     
      $now = Get-Date

     write-host ("Deleting Items from list started at " +$now.toString())
     
        while ($i -le ($count-1))
        {
            #write-host $i
            $item = $items[$i]
      
            $stringbuilder.AppendFormat("<Method ID=`"{0}`">", $i) > $null
            $stringbuilder.AppendFormat("<SetList Scope=`"Request`">{0}</SetList>", $list.ID) > $null
            $stringbuilder.AppendFormat("<SetVar Name=`"ID`">{0}</SetVar>", $item.Id) > $null
            $stringbuilder.Append("<SetVar Name=`"Cmd`">Delete</SetVar>") > $null
            $stringbuilder.Append("</Method>") > $null
     
            $i++
        }
        $stringbuilder.Append("</ows:Batch>") > $null
     
        $web.ProcessBatchData($stringbuilder.ToString()) > $null
    }
    catch
    {
        Write-Host -ForegroundColor Red $_.Exception.ToString()
    }
       $now = Get-Date

     write-host ("Deleting Items from list ended at " +$now.toString())
     write-host -ForegroundColor Green "done."

    **************************************End of Code***************************************************



    • Edited by cowboy2066 Thursday, February 12, 2015 6:39 PM
    Thursday, February 12, 2015 6:38 PM

Answers

  • Hi Cowboy,

    Per my knowledge, if we delete the workflow history list items, it will not affect the workflows waiting to be triggered.

    However, if you go to the workflow status page, the history will be empty as the events  have been deleted in workflow history list.

    The code in your post will delete 2000 items in workflow history list.

    As there is no status for the items in workflow history list, so I recommend to use the date as the query condition.

    For example, if you want to delete items created between 2015-02-01 and 2015-02-16, then you can change the query as below in your code:

    $query="<Where><And><Geq><FieldRef Name='Created'/><Value Type='DateTime'>2015-02-01T00:00:00Z</Value></Geq><Leq><FieldRef Name='Created' /><Value Type='DateTime'>2015-02-01T00:00:00Z</Value></Leq></And> </Where>"
    $spQuery=$query
    

    Thanks,

    Victoria

    Forum Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Victoria Xia
    TechNet Community Support

    Monday, February 16, 2015 3:47 AM