SharePoint Online: Delete List Items Created in the Past 7 Days using PowerShell
Requirement: Delete All List Items created within the past 7 days in SharePoint Online List.
PowerShell to Delete SharePoint Online List Items based on Created Date
PnP PowerShell to Delete List Items Created in the Past 7 Days:
While CAML method of filter list items is the recommended approach, here is an alternate approach to delete list items created in the past N number of days.
PowerShell to Delete SharePoint Online List Items based on Created Date
#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" #Parameters $SiteURL = "https://crescenttech.sharepoint.com" $UserName = "[email protected]" $Password = "Password" $SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force #Setup the Context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword) #Get the List $List = $Ctx.Web.Lists.GetByTitle("Documents") #Query to Get All List Items Created in the Past 7 Days $CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery $Query = "@ <View Scope='RecursiveAll'> <Query> <Where> <Geq> <FieldRef Name='Created' /> <Value Type='DateTime'><Today OffsetDays='-7'/></Value> </Geq> </Where> </Query> </View>" $CAMLQuery.ViewXml =$Query #Get List Items matching given conditions $ListItems = $List.GetItems($CAMLQuery) $Ctx.Load($ListItems) $Ctx.ExecuteQuery() #Delete List Items Write-host "Total Number of Items Matching:"$ListItems.Count Foreach ($Item in $ListItems) { $List.GetItemById($Item.id).Recycle() | Out-null Write-host "Deleted List Item:"$Item.Id } $Ctx.ExecuteQuery()
PnP PowerShell to Delete List Items Created in the Past 7 Days:
While CAML method of filter list items is the recommended approach, here is an alternate approach to delete list items created in the past N number of days.
#Variables $SiteURL = "https://crescenttech.sharepoint.com" $listName ="Documents" $UserName = "[email protected]" $Password = "Password1" $SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force $Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword #Connect to PNP Online Connect-PnPOnline -Url $SiteURL -Credentials $Cred #Get All List Items $ListItems = Get-PnPListItem -List $ListName -Fields "Title","Created","ID","GUID" #Set Cutoff Date - Past 7 days $CutoffDate = (Get-Date).AddDays(-7) ForEach($Item in $ListItems) { #Get the Created Date of the Item $CreatedDate = $Item["Created"] If($CreatedDate -ge $CutoffDate) { Remove-PnPListItem -List $ListName -Identity $Item.Id -Force -Recycle Write-Host "Deleted List Item:"$Item.Id } }
No comments:
Please Login and comment to get your questions answered!