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 »

  1. […] however the current script does not do this. I may just delete the list before import each time. Here is another PowerShell script to delete all items in a list, be careful to select the correct […]

    Pingback by More SharePoint 2013 Setup Tidbits | Crashcarr — March 10, 2014 @ 10:26 pm

  2. However I haven’t found a working script on your site 🙂
    But I see you’re my colleague in work with SharePoint

    Comment by admin — March 13, 2014 @ 2:16 am

RSS feed for comments on this post. TrackBack URL

Leave a comment





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