How to delete all items in a list (using Powershell)

This week, I needed to find a way to efficiently delete all items in a SharePoint list, with the least amount of customization and development effort. I found some code on the Internet (such as here). You can also use SP Designer, or download a custom webpart called SPPurgeList (see here).

Well, I wanted to do it via Powershell (for a number of reasons). Here’s the code that I came up with (and which works on my farm).

This Powershell script iterates through and deletes all items in the specified list.

# script starts here…

[System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”) | out-null

$oContentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService;

[Microsoft.SharePoint.Administration.SPWebApplicationCollection]$waColl = $oContentService.WebApplications;

$siteUrl = “ENTER YOUR SITE URL HERE”

$webName = “ENTER YOUR SUBWEB HERE”

$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)

$spWeb = $spSite.OpenWeb($webName)

$spList = $spWeb.Lists["ENTER YOUR LIST NAME HERE"]

foreach ($item in $spList.items)
{

$deaditem=$splist.GetItemById($item.ID)
$deaditem.Delete()

}

# Finished!

0 Responses to “How to delete all items in a list (using Powershell)”



  1. No Comments Yet

Leave a Reply




Archives

RSS Ben Curry’s Blog

  • Upgrading to SharePoint Server 2010 November 3, 2009
  • SharePoint Server 2010 Site Directory (where did it go?) October 29, 2009

Blog Stats

  • 3,115 hits