Skip to content

Instantly share code, notes, and snippets.

@kliemohn
Created April 26, 2016 20:25
Show Gist options
  • Save kliemohn/999d7e9f3c7f4c824b8367522feca1ee to your computer and use it in GitHub Desktop.
Save kliemohn/999d7e9f3c7f4c824b8367522feca1ee to your computer and use it in GitHub Desktop.
Deletes all workflow history list items from a specific workflow history list. Also deletes everything from the recycle bin. Note that this script has a bug where it continues after it should be done (infinite loop).
if ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null){
Add-PSSnapin -Name Microsoft.SharePoint.PowerShell
}
$url = "http://ccsdev/sites/psc2"
$site = New-Object Microsoft.SharePoint.SPSite($url)
$web = $site.OpenWeb()
try{
$list = $web.GetList("/sites/psc2/WorkflowHistory")
$cmd = [System.String]::Format("<Method><SetList Scope=`"Request`">{0}</SetList><SetVar Name=`"ID`">{{0}}</SetVar><SetVar Name=`"Cmd`">Delete</SetVar><SetVar Name=`"owsfileref`">{{1}}</SetVar></Method>", $list.ID);
$q = New-Object Microsoft.SharePoint.SPQuery
$q.RowLimit = 100
while($list.ItemCount -gt 0){
$items = $list.GetItems($q)
Write-Host "$(Get-Date): Deleting $($items.Count) items starting with ID $($items[0].ID)"
$sbDel = New-Object System.Text.StringBuilder
$sbDel.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>") | Out-Null
$ids = New-Object "System.Collections.Generic.List``1[System.Guid]"
for ($i=0; $i -lt $items.Count; $i++){
$item = $items[$i]
$delLine = [System.String]::Format($cmd, $item.ID.ToString(), $item.File.ServerRelativeUrl)
$sbDel.Append($delLine) | Out-Null
$ids.Add($item.UniqueId)
}
$sbDel.Append("</Batch>") | Out-Null
$web.ProcessBatchData($sbDel.ToString()) | Out-Null
# The line below always failed so we will manually clear the recycle bin
#$web.RecycleBin.Delete($ids.ToArray())
$web.RecycleBin.DeleteAll()
$list.Update()
}
}
finally{
if ($web -ne $null){
$web.Close()
}
if ($site -ne $null){
$site.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment