Different ways to delete all items from SharePoint list

This SharePoint tutorial, we will discuss how to delete all items from a SharePoint list using out of box feature as well as by using the SharePoint server object model. We will also see, how to delete all items from SharePoint list using PowerShell.

There might have some situation where you want to delete all items from a larger list that may have more than thousands of items. We will also see, how to delete multiple items from sharepoint list programmatically javascript.

Delete all items from a SharePoint list programmatically using the server object model

This example, we will discuss how to delete all items from a SharePoint list programmatically using the server object model.

protected void Page_Load(object sender, EventArgs e)
{
SPSite site = new SPSite("http://SiteURL");
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[List Name];

DeleteAllRerecords(list);
web.Update();
}

void DeleteAllRerecords(SPList list)
{
SPListItemCollection items = list.Items;
for (int i = items.Count – 1; i >= 0; i–)
{
items[i].Delete();
}
}

This is how we can delete all items from a SharePoint list programmatically using SharePoint server object model code.

Delete all items from SharePoint list using jsom

Let us see, how to delete all items from sharepoint list using jsom. The below code will work to delete multiple items from sharepoint list programmatically javascript. Also, we will see, how to delete a list item by id using the javascript object model (jsom) in SharePoint Online or SharePoint 2013/2016/2019.

Here we will see, how to delete all SharePoint list items using the JavaScript object model (jsom) in SharePoint Online Office 365. I have used to code inside a script editor web part inside a web part page in SharePoint.

Code:

<input type="button" id="btnDeleteItems" value="Click Here"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
$("#btnDeleteItems").click(function(){
DeleteAllItems();
});
});

function DeleteAllItems(){
var ctx = SP.ClientContext.get_current(),
list = ctx.get_web().get_lists().getByTitle('Ideas'),
query = new SP.CamlQuery(),
items = list.getItems(query);
ctx.load(items, "Include(Id)");
ctx.executeQueryAsync(function () {
var enumerator = items.getEnumerator(),
itemArray = [];
while (enumerator.moveNext()) {
itemArray.push(enumerator.get_current());
}

for (var s in itemArray) {
itemArray[s].deleteObject();
}
ctx.executeQueryAsync();
});
}
</script>

This will delete all items from the SharePoint Online list using jsom (JavaScript object model).

Delete all items from SharePoint online list using JavaScript object model

This is another way we can delete all items from a list using JavaScript object model (jsom) in SharePoint Online Office 365. The same code we can use to delete all items from SharePoint 2016 and SharePoint 2013 list.

Here in the CAML query, we are passing the RowLimit as 100, you can provide as per the requirement.

The below code we are putting inside a script editor web part which we have put inside a web part page.

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

delete all items from sharepoint list javascript
Delete all items from List using JavaScript Object Model (jsom) in SharePoint Online

The above code, we can use to delete multiple items from sharepoint list programmatically javascript.

Delete Items from SharePoint List using Out of box feature (for browsers other than IE like Chrome)

If you the open the list to delete all the items then you have to select all the items individually to delete. But if your list has more than thousands of items then it will be difficult to do this process. But you can do in the simplest approach.

Open the site and then click on the settings icon and then click on Site Settings. Then in the Site Settings page click on the “Content and structure” link under Site Administration section like below:

Delete all items from SharePoint list

Then from the Site Content and Structure page, Select the particular list whose items you wants to delete like below:

Delete all items from SharePoint online list using powershell

Then from the right-hand side click on the icons (shown in the yellow color) to select all the items and then click on the Delete option from the Actions button like below:

Delete all items from SharePoint list using powershell

This way you can delete all the items from the list.

For IE Browser:
If you are using IE browser, then first open the list view settings and then go to the Tabular View section in the down and select the checkbox “Allow individual item checkboxes “.

Delete all items from SharePoint 2016 list

Now you open the list and select the tick box (selected in yellow color) to select all the item like below:

Delete all items from SharePoint 2013 list

Delete Items from a SharePoint List using PowerShell

Now, we will discuss how to delete all list items using PowerShell in SharePoint 2013. The same PowerShell script we can use to delete all list items in SharePoint 2016.

We can write PowerShell script in Windows PowerShell ISE or you can use Visual studio code to write, debug and test PowerShell scripts.

Below is the PowerShell script. Open SharePoint Management Shell and run below PowerShell command.

I have a list name as “MyTestList99” which has below items. I want to delete all items using PowerShell.

Delete all items from SharePoint list programmatically
$web = Get-SPweb "http://win-pfcp2dgt8di/sites/EnjoySharePoint/"
$list = $web.Lists["MyTestList99"]
$items = $list.items
foreach ($item in $items)
{
$deleteitem=$list.GetItemById($item.ID)
$deleteitem.Delete()
}

After running above command, if you will refresh the SharePoint list, you will not get any item in it.

Delete all items from SharePoint list

This is how we can delete all SharePoint list items using PowerShell. The same code will work for SharePoint 2013/2016.

Delete all list items in SharePoint Online using PowerShell

Now, we will see, how to delete list items in SharePoint Online Office 365 using the Client Side Object Model (CSOM) with PowerShell. If you have more items to delete from SharePoint List, we can go for batch delete.

I would like to highlight programmatically when we delete list items, those are not moved to the Recycle Bin for the SharePoint site.

Please find the below code which will delete all items from the SharePoint list, the PowerShell script is hared by Sambita.

cls
Add-Type -Path "E:\PSDLL\Microsoft.SharePoint.Client.dll"
Add-Type -Path "E:\ PSDLL\Microsoft.SharePoint.Client.Runtime.dll"
$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$url = "https://onlysharepoint2013.sharepoint.com/sites/test123/"
$username = "[email protected]"
$password = "*****"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
#connect/authenticate to sharepoint online and get ClientContext object…
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
if(!$clientContext.ServerObjectIsNull.Value)
{
$web = $clientContext.Site.RootWeb
$clientContext.Load($web)
$clientContext.ExecuteQuery()
$list=$clientContext.Web.Lists.GetByTitle(‘TestList’)
$clientContext.Load($list)
$clientContext.ExecuteQuery()
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml="<View><RowLimit>1000</RowLimit></View>"
$items=$list.GetItems($query)
$clientContext.Load($items)
$clientContext.ExecuteQuery()

if ($items.Count -gt 0)
{
for ($i = $items.Count-1; $i -ge 0; $i–)
{
$items[$i].DeleteObject()
}
$clientContext.ExecuteQuery()
}
}

This is how we can delete all items from a SharePoint Online list using PowerShell.

You may like following SharePoint tutorials:

In this SharePoint tutorial, we learned, how to delete all items from a SharePoint list using the SharePoint server object model. Also, we saw how to delete all items from a SharePoint list using PowerShell and browser. And how to delete multiple items from sharepoint list programmatically javascript.

>