Delete list items created before N days using PowerShell in SharePoint Online

In this tutorial, we will discuss how to delete items created before N days from SharePoint online list using PowerShell. We will see here how we can use CAML to filter records based on created date and then we will delete those items.

In SharePoint Online, you might get some requirement to delete items that are created 30 days before or items that are created in the last 10 days like this.

In my previous post, we have discussed how to retrieve List Items Created in Last N days using PowerShell SharePoint Online.

Here we will extend the article and will check how we can delete the items.

Delete items created in last 7 days using PowerShell SharePoint online:

First, we will discuss how we can delete items created in last 7 days using PowerShell in SharePoint online.

Here I have a list in my SharePoint online site which has 3 items inside it, one item created a few days back and other two items created yesterday only. See the fig below:

delete all items from sharepoint list powershell
delete all items from sharepoint list powershell

Now according to our requirement, it should delete those two items which are created yesterday, because those two items only fall into items created in last 7 days condition.

In the PowerShell we have used CAML and we add filter using OffsetDays=-7 like below:

$caml = '<View><Query><Where>
<Gt>
<FieldRef Name="Created" Type="DateTime"/>
<Value Type="DateTime">
<Today OffsetDays="-7" />
</Value>
</Gt>
</Where></Query></View>'

Below is the full PoweShell Code. You can run the PowerShell script using Visual Studio code or Windows PowerShell ISE.

Try{
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"
$username = "bijay@onlysharepoint2013.onmicrosoft.com"
$password=ConvertTo-SecureString "******" -AsPlainText -Force
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$Web = $ctx.Web
$List = $web.get_lists().getByTitle("TrainingInformations")
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery;
$caml = '<View><Query><Where>
<Gt>
<FieldRef Name="Created" Type="DateTime"/>
<Value Type="DateTime">
<Today OffsetDays="-7" />
</Value>
</Gt>
</Where></Query></View>'
$Query.ViewXml =$caml
$ListItems = $list.GetItems($Query);
$ctx.Load($ListItems)
$ctx.ExecuteQuery()
$ListItems | ForEach-Object {
write-host $_["Title"]
}
foreach ($item in $ListItems)
{
$List.getItemById($item.id).deleteObject()
}
$ctx.ExecuteQuery()

Once you run the script, you can see the items created within last 7 days has been deleted like below:

SharePoint online delete items created in last 7 days using PowerShell
SharePoint online delete items created in last 7 days using PowerShell

Delete items created BEFORE last 7 days using PowerShell SharePoint online

Now we will see how we can delete items which are created before last 7 days using PowerShell in SharePoint online. In the above list, there is one item which is created before last 7 days.

Here we will slightly modify the CAML query and it should delete those items.

$caml = '<View><Query><Where>
<Lt>
<FieldRef Name="Created" Type="DateTime"/>
<Value Type="DateTime">
<Today OffsetDays="-7" />
</Value>
</Lt>
</Where></Query></View>'

Below is the full PowerShell script.

Try{
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"
$username = "bijay@onlysharepoint2013.onmicrosoft.com"
$password=ConvertTo-SecureString "*******" -AsPlainText -Force
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$Web = $ctx.Web
$List = $web.get_lists().getByTitle("TrainingInformations")
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery;
$caml = '<View><Query><Where>
<Lt>
<FieldRef Name="Created" Type="DateTime"/>
<Value Type="DateTime">
<Today OffsetDays="-7" />
</Value>
</Lt>
</Where></Query></View>'
$Query.ViewXml =$caml
$ListItems = $list.GetItems($Query);
$ctx.Load($ListItems)
$ctx.ExecuteQuery()
$ListItems | ForEach-Object {
write-host $_["Title"]
}
foreach ($item in $ListItems)
{
$List.getItemById($item.id).deleteObject()
}
$ctx.ExecuteQuery()

Once you run the above script, you can see the item will get deleted and see the output.

delete items created in last 7 days using PowerShell in SharePoint online
delete items created in last 7 days using PowerShell in SharePoint online

You can follow the below PowerShell tutorials:

I hope this tutorial helps us to delete list items created before N days using PowerShell in SharePoint Online.

>