SharePoint + Powershell: Remove Hold, Record Declaration on All Documents in a Library

In order to delete a Records Library or Records Center, all holds and records in a library must be removed and the Holds and Processing timer job must be run. If this criteria is not met, the "Delete this Document Library" will not be an option in the library settings.

Here is how you can do this programmatically. Note that I did not include the deletion of documents or the document library itself.

 1# Usage: .\Remove-Holds.ps1 <SiteUrl> <LibraryName>
 2$siteURL    = $args[0]
 3$libraryName = $args[1]
 4
 5$site    = Get-SPSite $siteURL
 6$web     = $site.RootWeb
 7$library = $web.Lists[$libraryName]
 8$records = $library.Items  # all items (was [0], which only returned the first item)
 9
10# First, remove all holds from all items in the Library
11$holdsList = $web.Lists["Holds"]
12$holds     = $holdsList.Items[0]
13[Microsoft.Office.RecordsManagement.Holds.Hold]::RemoveHold($records, $holds, "Holds have been removed.")
14
15# Next, undeclare all items as records.
16# Ps. BulkUndeclareItemsAsRecords is useless.
17foreach ($record in $records) {
18    [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::UndeclareItemAsRecord($record)
19}
20
21Start-SPTimerJob HoldProcessing
22
23$web.Dispose()
24$site.Dispose()

Thanks to anavijai for the sample Hold removing code.