Friday, October 23, 2015

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 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 = ""

$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()) 

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

    if ($ListItems.Count -gt 0)
        #Loop through each item and delete
        For ($i = $ListItems.Count-1; $i -ge 0; $i--)
        Write-Host "All List Items deleted Successfully!"
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.

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...