kwizcom banner advertisement

SharePoint Online: Delete All List Items using PowerShell

Requirement: SharePoint Online PowerShell to Delete List items
We've a list in SharePoint online - Office 365 site with bunch of items. Need them to be deleted in bulk rather deleting one by one from SharePoint web UI.
delete all list items in sharepoint online using powershell

Delete List Items in SharePoint Online using PowerShell:
Here is the script to delete all list items in SharePoint online Office 365 using client side object model (CSOM) with PowerShell.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/"
$ListName="Projects"

$UserName="admin@crescent.com
$Password ="Password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
$Context.Credentials = $credentials
  
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
$Context.Load($ListItems)
$Context.ExecuteQuery()       

write-host "Total Number of List Items found:"$ListItems.Count

    #sharepoint online powershell delete all list items
    if ($ListItems.Count -gt 0)
    {
        #Loop through each item and delete
        For ($i = $ListItems.Count-1; $i -ge 0; $i--)
        {
            $ListItems[$i].DeleteObject()
        } 
        $Context.ExecuteQuery()
        
        Write-Host "All List Items deleted Successfully!"
    }
 
This PowerShell script deletes all list items. Please note, these items are not moved to the Recycle Bin but deleted permanently! This PowerShell script could be quite helpful when dealing with deleting bulk items from a huge list.
SharePoint Online: Delete All List Items using PowerShell SharePoint Online: Delete All List Items using PowerShell Reviewed by Salaudeen Rajack on October 23, 2015 Rating: 5

4 comments:

  1. Hi
    Great article really helped. When running the script it gave an error when the list items are greater than 5000. Any idea how we can overcome this?

    ReplyDelete
    Replies
    1. For Large Lists with > 5000 list items, You should process items in batch as in here How to Get All List Items from Large Lists in SharePoint Online

      Delete
  2. How do I delete all Files (Not Folders)?

    ReplyDelete
    Replies
    1. Add an If condition:
      If($ListItem.FileSystemObjectType -eq "File")
      {
      #Delete the Item
      }

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.