Items bulk deletion

SP2013

Hello everyone,

Today I will talk about the massive removal of elements in SharePoint. Several possibilities exist, but the faster is the use of a batch via PowerShell.

You can find several websites / blogs on “how to create the batch” but for those I tested, I’ve always had a little something to change.

Sample sites :

http://bramnuyts.be/2013/05/16/remove-items-from-a-huge-sharepoint-list/

http://matthewyarlett.blogspot.com/2013/07/well-that-was-fun-bulk-deleting-items.html

http://sharepointnadeem.blogspot.com/2013/01/bulk-delete-items-in-sharepoint.html

http://florent.clairambault.fr/sharepoint-clean-a-huge-document-library

(by the way, thank you to all authors for their help in understanding the operation !)

In fact it is usually in the parameter “owsfileref” that divergent versions. Some do not specify, some  yes but other ways.

For me, here is the code that I use and that works for library documents and custom list.

Warning! Pay attention to the line

$batch=”

You can see that the values ​​1.0 and UTF-8 are not quoted with a simple  but with `”. I do not know why but the simple  raises an error « Exception calling « ProcessBatchData » with « 1 » argument(s) » when running the script!


Add-PSSnapin "Microsoft.SharePoint.PowerShell"

$urlSite = “http://serverurl”

$listname = “Test”

write-host "Opening site..."

$web=get-spweb $urlSite

write-host "Opening list..."

$list=$web.lists | ? { $_.title -eq $listName}

$spQuery= New-Object Microsoft.SharePoint.SPQuery

$spQuery.ViewAttributes="Scope='RecursiveAll'";

$spQuery.RowLimit = 2000

$caml = ""

$spQuery.Query=$caml

do

{

$listItems = $list.GetItems($spQuery)

$count=$listItems.Count

$spQuery.ListItemCollectionPosition= $listItems.ListItemCollectionPosition

$batch=""

$j=0

for($j=0;$j -lt $count; $j++)

{

$item=$listItems[$j]

$command=""

$command+="$($list.ID)"

$command+="$($item.ID)"

$command+="$($web.Url)/$($item.Url)"

$command+="Delete"

$command+=""

$batch+=$command

if($i -ge $count){break}

}

$batch+=""

write-host "Sending batch..."

$result=$web.ProcessBatchData($batch)

write-host "Emptying Recycle Bin..."

$web.RecycleBin.DeleteAll()

}

while($spQuery.ListItemCollectionPosition -ne $null)

$web.Dispose()

write-host "Process finished"

Hope this helps.

Christopher.

Advertisements
This entry was posted in SharePoint 2010, SharePoint 2013 and tagged , . Bookmark the permalink.

2 Responses to Items bulk deletion

  1. Cian says:

    HI – You have a typo – ListItemCollecitonPosition

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