Delete all items of the list using Client Object Model with PowerShell script for Office 365 sites. When you have more items to delete, we can go for batch delete.
I would like to highlight programmatically when we delete list items, those are not moved to the Recycle Bin for the site.
Please find the below code. Hope this will help.
cls
Add-Type-Path"E:\PSDLL\Microsoft.SharePoint.Client.dll"
Add-Type-Path"E:\PSDLL\Microsoft.SharePoint.Client.Runtime.dll"
$0=$MyInvocation.MyCommand.Definition
$dp0=[System.IO.Path]::GetDirectoryName($0)
$url="https://test.sharepoint.com/sites/test/"
$username="[email protected]"
$password="*****"
$securePassword=ConvertTo-SecureString$password-AsPlainText-Force
#connect/authenticate to sharepoint online and get ClientContext object...
$clientContext=New-ObjectMicrosoft.SharePoint.Client.ClientContext($url)
$credentials=New-ObjectMicrosoft.SharePoint.Client.SharePointOnlineCredentials($username,$securePassword)
$clientContext.Credentials=$credentials
if(!$clientContext.ServerObjectIsNull.Value)
{
$web=$clientContext.Site.RootWeb
$clientContext.Load($web)
$clientContext.ExecuteQuery()
$list=$clientContext.Web.Lists.GetByTitle('TestList')
$clientContext.Load($list)
$clientContext.ExecuteQuery()
$query=New-ObjectMicrosoft.SharePoint.Client.CamlQuery
$query.ViewXml="<View><RowLimit>1000</RowLimit></View>"
$items=$list.GetItems($query)
$clientContext.Load($items)
$clientContext.ExecuteQuery()
if ($items.Count-gt0)
{
for ($i=$items.Count-1; $i-ge0; $i--)
{
$items[$i].DeleteObject()
}
$clientContext.ExecuteQuery()
}
}