locked
SharePoint 2007 : How to programmatically delete all items in Document Library? RRS feed

  • Question

  • Hi All,

    I working in SharePoint 2007 environment, how can I programmatically delete all items within a Document Library?

    I tried below code, but hit error saying the item was change and unable to delete.

     

    using (SPSite site = new SPSite(_url))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        web.AllowUnsafeUpdates = true;

                        SPList documentLibrary = web.Lists["Testing"];

     

                        foreach (SPListItem item in documentLibrary.Items)

                        {

                            item.Delete();

                            item.Update();

                        }

                        web.AllowUnsafeUpdates = false;

                    }

                }

            }

    what is the best way to delete all items in a Document Library? 

    thank you in advanced.

    Tuesday, October 11, 2011 9:18 AM

Answers

  • Deleting all items inside List:


    SPWeb web = SPContext.Current.Web;
    SPList myList = web.Lists ["List_Name"];
    SPListItemCollection myListColl = web.Lists["List_Name "].Items;
    web.AllowUnsafeUpdates = true;
    for (int i = myListColl.Count - 1; i >= 0; i--)
    {
    myListColl [i].Delete();
    }
    web.Update();

    Reference: http://blogs.msdn.com/b/ketaanhs/archive/2008/02/07/sharepoint-2007-moss-sp-list-operations-using-object-model-progrmatically.aspx

    Try the above and let me know !


    Please mark answered if I've answered your question and vote for it as helpful to help other user's find a solution quicker!
    Tuesday, October 11, 2011 9:45 AM
  • You can also delete list item using this

     

    SPWeb mySite = SPContext.Current.Web;
    SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
    int itemCount = listItems.Count;

    for (int k=0; k<itemCount; k++)
    {
        SPListItem item = listItems[k];

        if (TextBox2.Text==item["Employee"].ToString())
        {
            listItems.Delete(k);
        }
    }


    Er.vinay
    • Marked as answer by Shimin Huang Wednesday, October 19, 2011 8:57 AM
    Tuesday, October 11, 2011 10:06 AM

All replies

  • Deleting all items inside List:


    SPWeb web = SPContext.Current.Web;
    SPList myList = web.Lists ["List_Name"];
    SPListItemCollection myListColl = web.Lists["List_Name "].Items;
    web.AllowUnsafeUpdates = true;
    for (int i = myListColl.Count - 1; i >= 0; i--)
    {
    myListColl [i].Delete();
    }
    web.Update();

    Reference: http://blogs.msdn.com/b/ketaanhs/archive/2008/02/07/sharepoint-2007-moss-sp-list-operations-using-object-model-progrmatically.aspx

    Try the above and let me know !


    Please mark answered if I've answered your question and vote for it as helpful to help other user's find a solution quicker!
    Tuesday, October 11, 2011 9:45 AM
  • You can also delete list item using this

     

    SPWeb mySite = SPContext.Current.Web;
    SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
    int itemCount = listItems.Count;

    for (int k=0; k<itemCount; k++)
    {
        SPListItem item = listItems[k];

        if (TextBox2.Text==item["Employee"].ToString())
        {
            listItems.Delete(k);
        }
    }


    Er.vinay
    • Marked as answer by Shimin Huang Wednesday, October 19, 2011 8:57 AM
    Tuesday, October 11, 2011 10:06 AM