Delete all items in SharePoint List using PowerShell




While testing SharePoint solution with SPLists I often need empty list. I'd like to have analog for "Truncate" in SQL, but there's nothing for SharePoint.

So I wrote a short PowerShell script for deleting all items in SharePoint List. Here it is

# "Enter the site URL instead http://serverurl" 
$SITEURL = "http://serverurl"

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

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

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

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

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

 

Using C# deleting like this - it must be from last to first element (Correct way to delete all SPListItems)




2 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment





MarkiMarta.com. Notes of web-specialist
Since 2009
18+