Skip to main content

Delete all Items from SharePoint Online List

 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"

     

#Config Parameters

$SiteURL="SharePoint Site URL"

$ListName="List Name"

$BatchSize = 500

   

#Setup Credentials to connect

$Cred = Get-Credential

   

Try {

    #Setup the context

    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)

    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

   

    #Get the web and List

    $Web = $Ctx.Web

    $List=$web.Lists.GetByTitle($ListName)

    $Ctx.Load($List)

    $Ctx.ExecuteQuery()

    Write-host "Total Number of Items Found in the List:"$List.ItemCount

  

    #Define CAML Query to get list items in batches

    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery

    $Query.ViewXml = "<View Scope='RecursiveAll'><RowLimit Paged='TRUE'>$BatchSize</RowLimit></View>"

  

    Do {  

        #Get items from the list in batches

        $ListItems = $List.GetItems($Query)

        $Ctx.Load($ListItems)

        $Ctx.ExecuteQuery()

          

        #Exit from Loop if No items found

        If($ListItems.count -eq 0) { Break; }

  

        Write-host Deleting $($ListItems.count) Items from the List...

  

        #Loop through each item and delete

        ForEach($Item in $ListItems)

        {

            $List.GetItemById($Item.Id).DeleteObject()

        } 

        $Ctx.ExecuteQuery()

  

    } While ($True)

  

    Write-host -f Green "All Items Deleted!"

}

Catch {

    write-host -f Red "Error Deleting List Items!" $_.Exception.Message

}

Comments

Popular posts from this blog

Key Limitations of Microsoft Power Automate (as of August 2025)

Microsoft Power Automate is a powerful tool for automating business processes, but like any platform, it comes with a set of limitations. Understanding these constraints is essential to designing efficient, scalable, and compliant workflows—especially as your automation strategy grows in complexity.  Here are the most important limits you need to know:  1. Switch Cases Each Switch action supports a maximum of 25 cases. If you need more, consider using nested Switches or alternate logic like parallel branches or conditionals.  2. Actions per Workflow A single flow can contain up to 500 actions. For complex workflows, you may need to split logic into separate flows or use child flows to stay within this limit.  3. Nesting Depth You can nest actions (e.g., conditionals or loops) up to 8 levels deep. Going beyond this will result in a design error.  4. Variables per Flow Each flow can define up to 250 variables. This includes all variable types (string, inte...

Bulk Import Excel Data to SharePoint List Using PowerShell and PnP

  Managing large datasets in SharePoint can be tricky, especially when you're dealing with Excel files and need to avoid list view threshold issues. In this guide, I’ll walk you through a PowerShell script that efficiently imports data from Excel into a SharePoint Online list using PnP PowerShell — with batching support for performance. Prerequisites Make sure you have the following before running the script: SharePoint Online site URL Excel file with data properly formatted PnP PowerShell module installed ( Install-Module PnP.PowerShell ) Appropriate SharePoint permissions What the Script Does Connects to your SharePoint site Loads and reads an Excel file Converts Excel date values Batches records in groups (to avoid the 5000 item threshold) Adds the items to your SharePoint list or library Logs execution time PowerShell Script $siteUrl = "[Site Collection URL]" Connect-PnPOnline -Url $siteUrl -UseWebLogin # Capture the start time $startTime...

Enable or Disable the Social Bar (Like, Views, Save for later) in SharePoint at tenant level

SharePoint Online provides various social features in modern experience SharePoint sites. One of the features available for SharePoint site pages is the social bar (Like, No. of Comments, Views, Save for later), which is situated at the bottom of site pages. Social bar allows users to engage with page content by liking and saving pages for later reference. Social bar also shows the number of page views and comments on modern site pages. However, organizations may have specific requirements that necessitate enabling or disabling the social bar on SharePoint site pages. Unfortunately, there are no settings available for enabling/disabling social bar using SharePoint user interface. In this blog post, we will explore how to achieve this at SharePoint tenant level using SharePoint Online PowerShell, PnP PowerShell and CLI for Microsoft 365 scripts. Using SharePoint Online PowerShell Use below SharePoint Online PowerShell script to enable or disable the social bar from site pages for all Sh...