Delete All Items from List or Library using PowerShell

At times, we may have to delete all items from SharePoint list or library. We can get it done by going to “Manage Content and Structure” option as in my another article How to Remove All responses from SharePoint Survey

There may be situations, where we’ve to delete all items from SharePoint list programmatically. Here are the code snippets to achieve the same.

Delete All Items from SharePoint List or Library using PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$WebURL = "https://portal.crescent.com/credit/"
$ListName="WorkflowHistory"

#Get web List and List Items
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]
$ListItems = $List.items
Write-host "Total Items in the List:"$List.ItemCount

#Iterate through each item in the list
foreach ($Item in $ListItems)
{
    $List.GetItemByID($Item.id).Delete()
    Write-host "Deleted Item: $($item.id)" -foregroundcolor Red
} 

You can also use the traditional way of deleting items from the end to avoid: “Collection was modified” Error:

For($i=$listItems.Count-1;$i -ge 0; $i--)
{
   Write-Host("Deleted: " + $listItems[$i].name)
   $listItems[$i].Delete()
}

One-Line PowerShell script to remove all items from SharePoint List:

(Get-SPWeb https://your-sharepoint-site).Lists["ListName"].Items | % { $_.ParentList.GetItemById($_.ID).Delete() }

Filter and Delete List Items:

We can filter the list items using CAML and perform the same delete operation. Here it goes:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

$url = "https://sharepoint.crescent.com/"
$web = Get-SPWeb $url
#Get the List
$list = $web.Lists["Tasks"]

#CAML Filter Query
$CAMLQuery="<Where><Eq><FieldRef Name='Status' /><Value Type='Choice'>Completed</Value></Eq></Where>"

$SPQuery=New-Object Microsoft.SharePoint.SPQuery
$SPQuery.ViewAttributes = "Scope='Recursive'"  #Get all items from Folders also!
$SPQuery.Query=$CAMLQuery
$SPQuery.RowLimit  = 5000; 

#Get the List items based on Filter 
$result=$list.GetItems($SPQuery)

#Delete the items from list
$result | % { $list.GetItemById($_.Id).Delete()  }

Delete All Items from Large SharePoint lists using PowerShell

When you try to delete large lists that exceed list view threshold limit, you may get a “The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.” error! To avoid this issue, we’ve to batch process list items. Here is how:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

#Parameters
$WebURL = "https://portal.crescent.com/credit"
$ListName="WorkflowHistory"

#Get web List and List Items
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]

$Query = New-Object Microsoft.SharePoint.SPQuery
$Query.ViewAttributes = "Scope='Recursive'"
$Query.RowLimit = 2000

#Delete List items
Do
{
    $ListItems = $List.GetItems($Query)
    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition

    Foreach($Item in $ListItems)
    {
        Write-Host "Deleting Item - $($Item.Id)"
        $List.GetItemById($Item.Id).delete()
    }
}
While ($Query.ListItemCollectionPosition -ne $null)

PowerShell Script to Delete All List Items in Bulk from a SharePoint List:

Here is how to bulk delete SharePoint list items using ProcessBatchData method.

#Parameters
$WebURL ="https://sharepoint.crescent.com/sites/operations/us/"
$ListName ="DSF_Transactions"
$BatchSize = 1000  #Lets delete 1000 items at a time

#Get the web object
$web = Get-SPWeb $WebURL
 
#Get the List
$list = $web.Lists[$ListName]

while ($list.ItemCount -gt 0)
{

    Write-host "Total Number of Items:"$list.itemcount
    
    $StringBuilder = New-Object System.Text.StringBuilder
    $StringBuilder.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>") > $null

    $BatchCount=0
    
    foreach($item in $List.Items)
    {
    $StringBuilder.Append("<Method><SetList Scope=`"Request`">$($list.ID)</SetList><SetVar Name=`"ID`">$($item.ID)</SetVar><SetVar Name=`"Cmd`">Delete</SetVar></Method>") > $null
    $BatchCount++
        if($BatchCount -ge $BatchSize) { break } #Break from this foreach loop
    } 

    $stringbuilder.Append("</ows:Batch>") > $null

    $web.ProcessBatchData($StringBuilder.ToString()) > $null
    $list.Update()
}

Don’t forget to Add, when you are deleting from a library:
<SetVar Name=`”owsfileref`”>$($item.File.ServerRelativeUrl)</SetVar>

Deleting SharePoint list items using ProcessBatchData method sends items to the Recycle bin!

Delete Vs Recycle:Will the Deleted Items goes to Recycle bin?
No! The delete method doesn’t send files to the Recycle bin. But the Recycle Method does! So, call the Recycle method, if you want the deleted items to be sent to the Recycle bin.

You can also: Save the list as a template (without content), Delete the list and Re-create from the list template! – Provided if there is no workflow/event receiver associated with the list.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

6 thoughts on “Delete All Items from List or Library using PowerShell

  • Hi Salaudeen

    I like to follow “Delete All Items from Large SharePoint lists using PowerShell” but how can I limit the number of items to delete for only items that are over 90 days old from the date they were created. Could you advice?

    Thanks

    Swanl98

    Reply
    • Use something like:

      #Past Month
      $dateFilter = (Get-Date).AddMonths(-1)

      If ($File.TimeCreated -lt $dateFilter)
      {
      #Do something
      }

      Reply
  • I want to delete files from library which were created on 8th jan between 10am to 1.30 pm and which were modified by system account. Can you help me out salaudeen

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *