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 list items created in the past 7 days in sharepoint online

PowerShell to Delete SharePoint Online List Items based on Created Date

Are you looking for a way to quickly delete all list items that were created in the past 7 days? PowerShell can help! In this guide, we’ll show you how to use PowerShell to delete all your old list items:

You can use PowerShell to delete items in a SharePoint Online list that were created in the past X days using the following script. This script connects to the SharePoint Online site and retrieves the items in the specified list using the SharePoint Client-Side Object Model (CSOM). The CAML query is then used to retrieve only the items created within the past X days. Finally, the script loops through each item and deletes it, providing a convenient and automated way to remove older items in your SharePoint list data.

#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://Crescent.sharepoint.com"
$UserName = "Salaudeen@CrescentTech.com"
$Password = "Password Goes Here"
$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 the CAML method of filtering 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://crescent.sharepoint.com"
$listName ="Documents"
$Days = -7
$UserName = "Salaudeen@TheCrescent.com"
$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" -PageSize 2000

#Set Cutoff Date - Past 7 days
$CutoffDate = (Get-Date).AddDays($Days)

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
    }
}

Replace the value of the variables as per your SharePoint Online site, list, etc. The $Days variable determines the number of days back from the current date to delete items. In this example, it is set to 7 days.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *