Bulk delete Items from SharePoint list Applies to: SharePoint Online & On-premises Description: This article describes steps to delete items from Sharepoint list. If there are many items in list and you only want to keep some of them then based on the ID value items can be deleted. SharePoint OnlinePre-requisites: 1. SharePoint Online Management Shell (link to download: https://www.microsoft.com/en-in/download/details.aspx?id=35588) and SharePoint Client Components SDK (https://www.microsoft.com/en-in/download/details.aspx?id=42038) 2. Install both of the above components in your machine (Run as Administrator) 3. To delete the items from send mails list you need “Contribute” permissions on the list 4. Script File to delete items (DeleteItems.ps1). Download DeleteItemsScript_Online. Extract the zip file and find the “DeleteItems.ps1” script file. Updates to Script File: 1. Open DeleteItems.ps1 and replace place holders with Site URL, List Name, User Name and Password.
2. Change $StartItemId and $EndItemId as per the ID range of items to delete. You can add ID column to the list view and sort the view by ID from highest to lowest to see the current maximum ID value. Similarly you can find the minimum value as well. Based on current max Id, you can decide how many to keep and then accordingly set the values for these variables: SharePoint On-premisesSteps: 1. Download DeleteItemsScript_OnPrem. Extract the zip file and find the DeleteItems.ps1 script file. 2. Open delete items script file and modify the “SiteURL” and “ListName” placeholders in delete items script file and save the file (see screenshot below) 3. Copy the delete items script file to the SharePoint server 4. Right click “DeleteItems.ps1” file and click “Run with Powershell”
Is there any way to delete items in a list/library from SharePoint 2010 but not only based on date created (default information policy options) but also on a combination of columns values in this list. Thank you 4
For SharePoint Online only, you can use Microsoft Graph and send in requests for each item on the list, deleting each item one by one (but quickly in a foreach loop).
/sites/{id}/lists/{id}/items
/sites/{id}/lists/{id}/items/{id} I do this with success as of today! Using my PowerShell module for MS Graph functions, https://www.powershellgallery.com/packages/MSGraphAppOnlyEssentials I get an Access Token using New-MSGraphAccessToken, then I use New-MSGraphQuery (which in a future version of the module will be renamed as 'New-MSGraphRequest'). You need to first setup an App Registration in Azure AD, and make sure it has the API permissions under Microsoft Graph API for Sites.ReadWrite.All. Then you need to generate a self-signed certificate and store this in CurrentUser/My, and export/upload/import the cert (no private key necessary) into the App Registration in Azure AD. https://docs.microsoft.com/en-us/graph/api/resources/listitem?view=graph-rest-1.0 https://docs.microsoft.com/en-us/graph/api/listitem-delete?view=graph-rest-1.0&tabs=http I would also be weary of the throttling limits, and considering putting a Start-Sleep in your foreach loop to avoid getting throttled.
This is an alternative to Jussi Palo's answer, but without iterating through ALL the list items, which is a huge performance loss. string filtervalue = TextBox1.Text; // Value you selected, criteria for deletion. SPWeb currentWeb = SPContext.Current.Web; SPList list = currentWeb.Lists.TryGetList("MyList"); if(list==null) throw new InvalidOperationException("MyList does not exist on current site."); SPQuery query = new SPQuery() { Query = string.Format("<Where><Eq><FieldRef Name='MyField' /><Value Type='Text'>{0}</Value></Eq></Where>", filtervalue), ViewFields = "<FieldRef Name='ID' />", ViewFieldsOnly = true }; SPListItemCollection filteredItems = list.GetItems(query); foreach (SPListItem item in filteredItems) { item.Delete(); break; // If you want to delete ONLY the first occurence. } list.Update();Also, if by any chance you know the ID of the item you selected, you can pass that as a parameter to the following method: list.Items.DeleteItemById(SelectedId);
In 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 the SharePoint list using PowerShell. There might have some situations 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 the sharepoint list programmatically javascript. In this example, we will discuss how to delete all items from a SharePoint list programmatically using the server object model.
Complete SharePoint Training Course Bundle for $199 (Just for Today) 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. 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: The above code, we can use to delete multiple items from sharepoint list programmatically javascript. If you 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 it in the simplest approach. Open the site and then click on the settings icon and then click on Site Settings. Then on the Site Settings page click on the “Content and structure” link under Site Administration section like below: Then from the Site Content and Structure page, Select the particular list whose items you wants to delete like below: 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: This way you can delete all the items from the list. For IE Browser: Now you open the list and select the tick box (selected in yellow color) to select all the item like below: 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 the below items. I want to delete all items using PowerShell. After running above command, if you will refresh the SharePoint list, you will not get any item in it. This is how we can delete all SharePoint list items using PowerShell. The same code will work for SharePoint 2013/2016. 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 the 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. I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SPGuides.com |