In this PnP PowerShell tutorial, we will discuss, how to delete all items from a SharePoint Online list using PnP PowerShell.
We will see, how to delete list items created before N days using PowerShell in SharePoint Online.
If you are new to PnP PowerShell, you can read an article on How to connect SharePoint Online using PnP PowerShell.
Delete all sharepoint list items using PnP PowerShell
Here, I got a requirement to delete more than 10000 items from a SharePoint Online list.
I used PnP PowerShell to delete items from the SharePoint Online list.
Below is the PowerShell command to remove all items from a SharePoint Online list using PnP PowerShell, you can write, debug and test the script using PowerShell ISE.
Connect-PnPOnline –Url https://tsinfo.sharepoint.com/sites/sharepointsky/
$items =Get-PnPListItem -List "Budgets" -PageSize 500
foreach ($item in $items)
{
try
{
Remove-PnPListItem -List "Budgets" -Identity $item.Id -Force
}
catch
{
Write-Host "Error Occurred While Deleting the Item from the SharePoint Online List"
}
}
This will delete all the items from the list.
It will take really good amount of time if it is a large list, or is having more items in the SharePoint list.
Here I have added the -PageSize 500 attribute.
Get-PnPListItem : The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator
If you will not use the -PageSize attribute, then you will get the error like below:
Get-PnPListItem : The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.

The above error comes because of the SharePoint list view threshold issue. To fix the error you can add the -PageSize like below:
$items =Get-PnPListItem -List "Budgets" -PageSize 10000
This is how to fix the error, get-pnplistitem : the attempted operation is prohibited because it exceeds the list view threshold.
Read: SharePoint PnP PowerShell examples
Delete SharePoint list items programmatically using JSOM
Now, let us see, how to delete all items from the SharePoint list programmatically javascript object model (jsom) in SharePoint Online.
In SharePoint Online we can only use client-side object model code, we can not use any server-side code.
In this particular example, we got a requirement to delete all items from a SharePoint online list using the JavaScript object model (jsom).
Here we are using a SharePoint online site Office 365 but the same code will also work for SharePoint 2016 and SharePoint 2013.
In this jsom example I have taken a button and on click of that button it will delete all items from a list known as “SourceList”. And I have added the jsom code inside a script editor web part which we have put inside a web part page in SharePoint Online.
If you are new to JSOM (JavaScript Object Model), then you can download a FREE PDF Of 51 JSOM Examples in SharePoint Online.
Here in the CAML query, we are passing the RowLimit as 100, you can provide as per the requirement.
Delete all items from SharePoint list programmatically javascript client object model (jsom)
Below is the full JavaSctipt Object model code to delete all items from the sharepoint list programmatically.
<input type="button" id="btnSubmit" value="Delete All Items" /><br/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function () {
bindButtonClick();
});
function bindButtonClick() {
$("#btnSubmit").on("click", function () {
deleteAllItemsFromList();
});
}
var clientContext;
var website;
var oList;
var cnt = 0;
function deleteAllItemsFromList() {
clientContext = SP.ClientContext.get_current();
website = clientContext.get_web();
oList = website.get_lists().getByTitle('SourceList');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(website);
clientContext.load(collListItem, 'Include(Id)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var ID = oListItem.get_id();
var oListItemDel = oList.getItemById(ID);
oListItemDel.deleteObject();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onDeleteSucceeded), Function.createDelegate(this, this.onDeleteFailed));
}
}
function onQueryFailed(sender, args) {
alert('Failed');
}
function onDeleteFailed(sender, args) {
alert('Failed');
}
function onDeleteSucceeded(sender, args) {
cnt = cnt + 1;
alert('Delete success : ' + cnt);
}
</script>
Once you will save the code and click on the button to delete, it will display an alert on each item delete like below:

Here, we saw, how to delete all items from sharepoint list programmatically javascript or jsom in SharePoint Online or SharePoint 2019/2016/2013.
SharePoint pnp delete all list items
Now, let us see how to all list items using PnP in SharePoint Online. The same pnp code we can use to delete list items from SharePoint 2013, SharePoint 2016 and SharePoint 2019.
If you are new to pnp SharePoint Online, read SharePoint Online Development using Patterns and Practices (PnP).
SharePoint online pnp delete list items
On my SharePoint Online site, I have a list name called “Order Details” with few items. We will see how we can delete all items from this SharePoint Online list programmatically using PnP core CSOM library code.

The below references we have to use in the code to work with PnP libraries:
- Microsoft.SharePoint.Client
- OfficeDevPnP.Core
Here I have created an asp.net application and I have added a button. Just click on the button, it will Delete all items from the list of SharePoint Online using the PnP core CSOM library.
<div>
<asp:Button ID="btnDeleteItems" runat="server" Text="Delete Items From the List" OnClick="btnDeleteItems_Click" /><br />
<asp:Label ID="lblDeleteItems" runat="server" Text=""></asp:Label>
</div>
Below given snippet code
AuthenticationManager authMgr = new AuthenticationManager();
string siteURL = https://DomainName.sharepoint.com/sites/TSInfoPNP";
string userName = "*******@DomainName.onmicrosoft.com";
string password = "******";
protected void btnDeleteItems_Click(object sender, EventArgs e)
{
deleteItemsInList();
}
void deleteItemsInList()
{
try
{
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteURL, userName, password))
{
List oList = ctx.Web.GetListByTitle("Order Details");
ctx.ExecuteQueryRetry();
CamlQuery query = CamlQuery.CreateAllItemsQuery(4000); Microsoft.SharePoint.Client.ListItemCollection items = oList.GetItems(query);
ctx.Load(items);
ctx.ExecuteQuery();
for (int i = items.Count - 1; i >= 0; i--)
{
items[i].DeleteObject();
}
ctx.ExecuteQuery();
lblDeleteItems.Text = "All Items Deleted from the List Successfully";
}
}
catch (Exception ex)
{
}
}
Run your application and click on button on the page.

Go and check your SharePoint Online list all the items are deleted as shown below.

Here, we learned, how to delete all items programmatically from
Delete items created in last 7 days using PowerShell SharePoint online
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.
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:

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:

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.

This is how to delete items created before N days from SharePoint online list using PowerShell.
You may like following PnP PowerShell tutorials:
- Connect-PnPOnline : The ‘Connect-PnPOnline’ command was found in the module ‘SharePointPnPPowerShellOnline’
- The term ‘Get-MsolUser’ is not recognized as the name of a cmdlet
- Microsoft Lists – Create a List from Excel
- SharePoint download multiple files + using PnP PowerShell
In this tutorial we learned how to Remove all items from a SharePoint Online list using PnP PowerShell and how to fix error Get-PnPListItem : The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com