Delete Sharepoint List Programmaticaly

In this article I am explaining how to delete Sharepoint List Programmatically.

To delete any object in sharepoint, you generally need to call Delete() method of the object.

To delete Sharepoint List
Open Visual studio - File - Create New project
then select C# as the language and Console Application from the panel.
Give DeleteList as name for project, then click ok.


After Creating the project, you need to add the Sharepoint DLL for your project.
For this right click on solution Explorer and select Add Reference . Browse for Microsoft.Sharepoint.dll file and click ok.
With default installation of Sharepoint, you can find this dll file at below location.

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll

So, you successfully establish the environment to delete List in Sharepoint.
The code to delete Sharepoint List is shown below.


    
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; namespace DeleteList { class Program { static void Main(string[] args) { SPSite site = new SPSite("http://home-pt72rbbzr0"); SPWeb web = site.AllWebs[0]; SPList list = web.Lists["New Task List"]; list.Delete(); Console.WriteLine("Lists Deleted Successfully"); Console.ReadLine(); } } }

In the above code, if you observe we created the SPSite object for root site i.e, http://home-pt72rbbzr0 (In my System I have root URL as like this, you may have different root site URL. Please mention your sharepoint root site URL to create site for this root site)
After that I am refereing the first web of the root site by mentioning index as 0. If you want you can mention web name instead of index.
Now we need to get the required List which one you want delete(here I want to delete New Task List)

We write the code to delet List, then we need to deploy it on production server.
If the production server is same as develop server, then just press F5, if not you need to copy the entire project to production server and install or deploying your code using various deployment methods in .Net Framework and install on server.


After completing the installation, you will get the following window.

Deleting Sharepoint List

open new Browser window with the new site URL(i.e. http://home-pt72rbbzr0), and click on "view All Site Contenet" as shown below.

Sharepoint List

After that go to All site content dropdown List and select "Lists" field as shown below.

Sharepoint List

Then you can observe your "New Task List" was not there, because we deleted that one.

Delete Sharepoint List

In this way we can delete any sharepoint List just by calling Delete() method of the List.


Download source code here