Safely purge items from history list - PowerShell

Document created by eharris04 Employee on Mar 30, 2016
Version 1Show Document
  • View in full screen mode

This PowerShell script allows you to safely target and purge items from a workflow history list to keep the size down and manageable.

 

AuthorAaron Labiosa
Long DescriptionIn order to get rid of the items and not impact the entire farm, it becomes necessary to utilize paging and indexing to specifically target each item and delete it. Paging helps throttle the traffic to your SQL server down by only deleting x number of items at a time before it rests and starts again. Indexing enables the targeting of items without the performance overhead of enumerating and/or querying a large collection of items.
Dependencies

To use the script do the following:

 

 

  • Replace http://contoso.com with the URL of the site you wish to execute the script against.
  • Replace NintexWorkflowHistory with the title of the history list you wish to target.

Note: By default the script will delete 1000 items and then rest for 1 second.

Support InfoAaron Labiosa - How to purge items from a large history list safely via PowerShell
Compatibility

Nintex Workflow 2010

Nintex Workflow 2013

Platform

SharePoint Server 2010

SharePoint Server 2013

Screenshots
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Configure target site and list.
$list = $($(Get-SPWeb -Identity 'http://contoso.com').Lists['NintexWorkflowHistory'])
#Index count for list items.
$index = $list.ItemCount
#Index counter for paging.
$page = 0
#Configure how many items to delete per batch.
$pagesize = 1000
#Configure how may seconds to pause between batches.
$sleep = 1
#Turn verbose output on/off
$verbose = $true
While($index -ge 0){
if($verbose){
$("Deleting item at index $($index).")
}
if($page -lt $pagesize){
try{
if($($list.Items[$index])['Modified'] -lt [DateTime]::Parse("01/01/2014")){
$list.Items[$index].Delete()
write-host "Found Item"
}
}
catch [System.Exception]{
if($verbose){
$("Skipping item at index $($index).")
}
}
$index--
$page++
}
else{
if($verbose){
$("Sleeping for $($sleep) seconds.")
}
[System.Threading.Thread]::Sleep($sleep * 1000)
$page = 0
}
}

Additional Information

Filtering can be added by adding an if statement around the Delete() call as shown below. In this example, the item would be deleted if it was older than 01/01/1999.

try{
if((($list.Items[$index])[($list.Items[$index].Fields['Created'])] -lt [DateTime]::Parse("01/01/1999")))
{
$list.Items[$index].Delete()
}

Nintex Xchange Terms of Use Policy

Nintex makes no warranty or guarantee about the reliability, performance, quality, or functionality of any assets, and any assets you install are therefore provided as is. By downloading this asset, you agree to the terms of use.

 

Attachments

Outcomes