Freigeben über


PowerShell script to delete all items in a List

Another script I wrote is to delete all items from a List.

###################################################################################################

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

# "Enter the site URL here"
$SITEURL = "https://serverurl"

$site = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$web = $site.OpenWeb()
"Web is : " + $web.Title

# Enter name of the List below
$oList = $web.Lists["ENTER LIST NAME HERE"];

"List is :" + $oList.Title + " with item count " + $oList.ItemCount

$collListItems = $oList.Items;
$count = $collListItems.Count - 1

for($intIndex = $count; $intIndex -gt -1; $intIndex--)
{
        "Deleting : " + $intIndex
        $collListItems.Delete($intIndex);
}

###################################################################################################

Disclaimer: The above code is provided "As Is". This is just a sample code and is not production ready.

Comments

Delete an item from the list by using a CAML query Within a specifi range

[string] $web = "http://abc.abc.com/test"; #Considering Workflow history as an example [string] $list = "Workflow History"; [string] $caml = "<Where><Lt><FieldRef Name=&quot;Modified"/><Value Type=&quot;DateTime">2011-3-1T00:00:00Z</Value></Lt></Where><OrderBy><FieldRef Name=&quot;ID" Ascending=&quot;TRUE"/></OrderBy>"; #Number of items ot delete in a batch [int] $rowLimit = 5000; [switch] $whatif = $false; [System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") $spsite = New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList $web $spweb = $spsite.OpenWeb() $splist = $spweb.Lists[$list] $query=new-object -TypeName "Microsoft.SharePoint.SPQuery" $query.Query = $caml $query.RowLimit = $rowLimit $col = $null $col=$splist.GetItems($query); $count = $col.Count; Write-Host ("Items Count to be Deleted = " + $count)

Create batch remove CAML query

 $batchRemove = '<?xml version="1.0" encoding="UTF-8"?><Batch>';  # The command is used for each list item retrieved  $command = '<Method><SetList Scope="Request">' + $splist.ID +'</SetList><SetVar Name="ID">{0}</SetVar>’ + ‘<SetVar Name="Cmd">Delete</SetVar></Method>';  foreach ($item in $col)  {    # Loop through each list item and add the string to the batch command    $batchRemove += $command -f $item.Id;  }  $batchRemove += "</Batch>";  #Write-Host ("BATCH  " + $batchRemove)  # Remove the list items using the batch command  $spList.ParentWeb.ProcessBatchData($batchRemove) | Out-Null $spweb.Dispose() $spsite.Dispose()