Delete selected Items from sharepoint list programmatically

Introduction

There are few ways to delete item from sharepoint list, however I like the way to perform ProcessBatchData method (approach 1 in this article) which seems faster to me.

Here I am going to explain about 2 approaches in which we will  use SPListItem.Delete or SPWeb.ProcessBatchData method. My example will delete items from list if DeptID match with list item.

Approach 1 (SPWeb.ProcessBatchData):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/// get SPListItemCollection using spquery.
/// generate query for all items
/// Pass query into ProcessBatchData method.
Public void DeleteItemUsingBatch(string deptId)
{
    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
    {
	SPWeb web = site.OpenWeb();
	web.AllowUnsafeUpdates = true;
	SPList list = web.Lists[“Departments”];
	SPQuery query = new SPQuery();
	query.Query = "<Where> <Eq><FieldRef Name=’deptId’ /><Value Type=’Lookup’>" + 
                         deptId + "</Value> </Eq></Where>";
	SPListItemCollection listItem = list.GetItems(query);
	StringBuilder sbDelete = new StringBuilder();
	string xmlFormat = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	sbDelete.Append(xmlFormat);
	sbDelete.Append("<Batch>");
	string buildQuery = "<Method><SetList Scope=\"Request\">" + list.ID + "</SetList>";
	buildQuery = buildQuery +
	"<SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
	foreach (SPListItem item in listItem)
	{
		sbDelete.Append(string.Format(buildQuery, item.ID.ToString()));
	}
	sbDelete.Append("</Batch>");
	web.ProcessBatchData(sbDelete.ToString());
    }
}

Approach 2 (SPListItem.Delete):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/// get SPListItemCollection using spquery.
/// Create loop as per SPListItemCollection.count
/// in loop Delete item using SPListItem.delete.
Public void DeleteItemWithoutBatch(string deptId)
{
	SPWeb web = site.OpenWeb();
	web.AllowUnsafeUpdates = true;
	SPList list = web.Lists[“Departments”];
	SPQuery query = new SPQuery();
	query.Query = "<Where> <Eq><FieldRef Name=’deptId’ /><Value Type=’Lookup’>" + 
                       deptId + "</Value> </Eq></Where>";
	SPListItemCollection listItems = list.GetItems(query);
	int itemCount = listItems.Count;
	for (int k=0; k<itemCount; k++)
	{
		SPListItem item = listItems[k];
		listItems.Delete(k);
	}
}

delete entire sharepoint list (Approach 1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/// To delete list you need 2 things
/// 1. Get SPListCollection object
/// 2. Get SPList object for list which need to be deleted.
/// Then you can use like this <SPListCollection object>.Delete(SPList.ID)
SPList list;
SPListCollection lists;
using (SPSite site = new SPSite("site url"))
{
	using (SPWeb web = site.OpenWeb())
	{
		lists = web.Lists;
		string listPath = "/lists/testlist"; // you can write your list path
		list = web.GetList(listPath);
	}
}
System.Guid listGuid = list.ID; 
lists.Delete(listGuid);

delete entire sharepoint list (Approach 2)

1
2
3
4
5
6
7
8
9
10
11
12
13
/// To delete list you need 2 things
/// 1. Get SPList object for list which need to be deleted
/// 2. Then you can use like this <SPList object>.Delete()
SPList list;
using (SPSite site = new SPSite("site url"))
{
	using (SPWeb web = site.OpenWeb())
	{			
		string listPath = "/lists/testlist"; // you can write your list path
		list = web.GetList(listPath);
	}
}
list.Delete();

You may also like below articles:-

how to update items from sharepoint list programmatically
how to add items from sharepoint list programmatically
Get sharepoint list item programmatically

Hope it helps.
Thanks & Regards,
Avinash

calendarApril 18, 2012 · cardInfoyen · comments8 Comments
tagTags: , , , , , , ,  · Posted in: List, MOSS

8 Responses

  1. Infoyen - June 21, 2012

    delete all items from sharepoint list programmatically

    You can use above functions to delete all items from sharepoint list. To do so you need to assign empty string in caml query. see below

    query.Query = string.empty;
  2. Updating a List Item programmatically | SharePoint Solutions - June 21, 2012

    Add, Update and Delete List Items Programmatically in Sharepoint

    how to delete items from sharepoint list programmatically
    how to update items from sharepoint list programmatically
    how to add items from sharepoint list programmatically

  3. Dorababu - November 12, 2012

    How to delete entire list..

  4. Infoyen - November 12, 2012

    you can delete list in sharepoint 2010 programmatically, as mentioned below:-

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    SPList list;
    SPListCollection lists;
    	using (SPSite site = new SPSite("site url"))
    	{
    		using (SPWeb web = site.OpenWeb())
    		{
    			lists = web.Lists;
    			string listPath = "/lists/testlist"; // you can write your list path
    			list = web.GetList(listPath);
    		}
    	}
    System.Guid listGuid = list.ID; 
    lists.Delete(listGuid);
  5. Shona Jay - March 11, 2014

    Hello I tried the this logic to delete items form the given list based on the particular item condition …but it is not working
    listName = list name by user
    colName = Column name by user
    colValue = Value

    I want to delete items from the listName where colName = colValue.

    web.AllowUnsafeUpdates = true;
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var webLists = web.get_lists();
    var desiredList = webLists.getByTitle(listName);
    //var desiredListItems = desiredList.getItems(query);
    //ctx.load(desiredListItems);

    SPQuery query = new SPQuery();
    query.Query = “”;
    SPListItemCollection listItems = list.GetItems(query);
    int itemCount = listItems.Count;
    for (int k=0; k<itemCount; k++)
    {
    SPListItem item = listItems[k];
    listItems.Delete(k);
    }
    ctx.executeQueryAsync(success, error);
    web.AllowUnsafeUpdates = false;
    }

  6. Shona Jay - March 11, 2014

    Hello I tried the this logic to delete items form the given list based on the particular item condition …but it is not working…

  7. Shona Jay - March 11, 2014

    following is my code …..
    listName = list name by user
    colName = Column name by user
    colValue = Value
    I want to delete items from the listName where colName = colValue.

    web.AllowUnsafeUpdates = true;
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var webLists = web.get_lists();
    var desiredList = webLists.getByTitle(listName);
    //var desiredListItems = desiredList.getItems(query);
    //ctx.load(desiredListItems);

    SPQuery query = new SPQuery();
    query.Query = “”;
    SPListItemCollection listItems = list.GetItems(query);
    int itemCount = listItems.Count;
    for (int k=0; k<itemCount; k++)
    {
    SPListItem item = listItems[k];
    listItems.Delete(k);
    }
    ctx.executeQueryAsync(success, error);
    web.AllowUnsafeUpdates = false;
    }

  8. Infoyen - June 18, 2014

    Hi Shona Jay,

    I have executed same at my server. It works.
    Please try again. You may have some spelling mistakes in your site url, list name, column name etc..

    Let me know if you still have problem.

    Thanks!!

Leave a Reply

Spam Protection: , required

myworldmaps infoyen