How to Delete All Items From Sharepoint List Using Powershell

When we do a lot of SharePoint consultant work. We need to clear out what we worked on and try it again. Especially if we are doing any kind of SharePoint migration. In this example, we use a Powershell command to delete all the items from SharePoint list programmatically. No manual work is required. Give it a try

###################################################################################################
[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://test.org/Blogs/"
$site = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$web = $site.OpenWeb()
"Web is : " + $web.Title
# Enter name of the List below
$oList = $web.Lists["Posts"];
"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);
}
###################################################################################################

Save this code as a .ps1 file and run it in SharePoint Powershell and you are go to go.

If you have any questions or comments please leave them in the comments section below.

We are always available to help your business with any SharePoint consultant work. If you are interested in learning more please contact us here.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: