You can try the below code:
#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"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$ListName="Projects"
$BatchSize = 500
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#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
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View><RowLimit>$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
}
To delete items using pnp powershell, try the below code,
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$ListName ="Records"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Get All List Items in Batch
$ListItems = Get-PnPListItem -List $ListName -PageSize 1000
#Loop through List Items and Delete
ForEach ($Item in $ListItems)
{
Remove-PnPListItem -List $ListName -Identity $Item.Id -Force
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Source: https://www.sharepointdiary.com/2015/10/delete-all-list-items-in-sharepoint-online-using-powershell.html
And also have a look at this link:
Deleting a Very Large SharePoint List.