Have you ever wanted a way to quickly purge a SharePoint List or Document Library? With some help from this blog post I was able to come up with a Powershell script that you can use to empty a SharePoint List very quickly. This example also deletes all items recursively keeping the folder structure intact.

Start-Transcript ‘c:\powershell\logs\transcript.txt’
[System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

$site = new-object Microsoft.SharePoint.SPSite(“http://localhost”)
$web = $site.rootweb
$list = $web.Lists["Name of your list that needs cleansing"]

$caml='<Where><Eq><FieldRef Name=”ContentType” /><Value Type=”Text”>Item</Value></Eq></Where>’

$query=new-object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = “Scope=’Recursive'”
$query.Query=$caml  | Write-Output

$items=$list.GetItems($query)

# Pipe results to a loop and delete each element

$items | % { $_.Delete() }

$web.Dispose()
$site.Dispose()
Stop-Transcript

Much of the magic is in the CAML query as that is where you define what you’re looking to delete as well as if you want it to work recursively down the List structure or not. Cheers!

About these ads