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!

Advertisements

1 Response to “How to delete all items in a list (using Powershell)”


  1. 1 Shivank Kaul October 24, 2013 at 1:02 am

    Thanks man, i was doing –
    foreach ($item in $spList.items)
    {
    item.Delete()
    }
    and this was causing me unfathomable grief..


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




RSS Information Week Headlines

  • An error has occurred; the feed is probably down. Try again later.

RSS SharePoint Team Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS InfoPath Team Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Joel Oleson Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Susan Hanley’s KM Blog

  • An error has occurred; the feed is probably down. Try again later.

Blog Stats

  • 348,120 hits

%d bloggers like this: