SharePoint – Workflow History Cleanup

After having your SharePoint in the air for a couple of years, you might be surprised how many records you have in the Workflow History List ?

image

So time to do some cleaning, because SharePoint is not going to do it for you.

cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) ´
-eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$sourceWebURL = “http://YourSite/”
$sourceListName = "Workflow History"

$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
#$spSourceItems = $spSourceList.GetItems()
#$spSourceItems = $spSourceList.GetItemById("1")
$spSourceItems = $spSourceList.Items | where {$_['Created'] `
-gt (get-date).AddDays(-60)} 

write-host $spSourceList.Items.count "in the list"
write-host " "

$i = 0

$spSourceItems | ForEach-Object {
    Write-Host $_['WorkflowAssociation'] $_['List'] $_['Created']
    $itemID = $spSourceList.GetItemById($_['ID'])
    $itemID.delete()
    # $itemID.Recycle() # Moves it to the recyclebin instead
    $i++
}

write-host " "
write-host "$i Selected"

Enjoy !

Advertisements

2 Responses to SharePoint – Workflow History Cleanup

  1. Brian Hayes says:

    Line 14 with the “-gt”, shouldn’t that be “-lt” if you want to delete everything over 60 days old? Maybe that was your intention, but I only want to delete things over 60 days.

  2. Hi Brian,

    Actually the line says “-gt” Minus 60 days 😉
    Giving the same result, It’s just a matter of presentation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: