Friday, 27 November 2015

Powershell empty large SharePoint 2013 lists




  1. param($weburl,$listname)
  2. if ($weburl -eq $null -or $listname -eq $null)
  3. {
  4.     write-host -foregroundcolor red "-weburl or -listname are null."
  5.     return
  6. }
  7. Add-PSSnapin Microsoft.SharePoint.Powershell -EA 0
  8. $web = get-spweb $weburl
  9. $list = $web.lists[$listname]
  10. $stringbuilder = new-object System.Text.StringBuilder
  11.  
  12. try
  13. {
  14.     $stringbuilder.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><ows:Batch OnError=`"Return`">") > $null
  15.     $i=0
  16.     $spQuery = New-Object Microsoft.SharePoint.SPQuery
  17.     $spQuery.ViewFieldsOnly = $true
  18.     $items = $list.GetItems($spQuery);
  19.     $count = $items.Count
  20.     while ($i -le ($count-1))
  21.     {
  22.         write-host $i
  23.         $item = $items[$i]
  24.         $stringbuilder.AppendFormat("<Method ID=`"{0}`">", $i) > $null
  25.         $stringbuilder.AppendFormat("<SetList Scope=`"Request`">{0}</SetList>", $list.ID) > $null
  26.         $stringbuilder.AppendFormat("<SetVar Name=`"ID`">{0}</SetVar>", $item.Id) > $null
  27.         $stringbuilder.Append("<SetVar Name=`"Cmd`">Delete</SetVar>") > $null
  28.         $stringbuilder.Append("</Method>") > $null
  29.         $i++
  30.     }
  31.     $stringbuilder.Append("</ows:Batch>") > $null
  32.     $web.ProcessBatchData($stringbuilder.ToString()) > $null
  33. }
  34. catch
  35. {
  36.     Write-Host -ForegroundColor Red $_.Exception.ToString()
  37. }
  38. write-host -ForegroundColor Green "done."


Use

Delete-Items.ps1 -weburl [url of web] -listname [name of list]

No comments:

Post a Comment