Lars Nielsen's Discoveries

May 28, 2010

How to delete a SharePoint list

Filed under: Development,SharePoint — Lars Nielsen @ 8:00 pm
Tags: ,

Here’s a short note about deleting lists programatically using the object model in SharePoint.

I had a look at the SPList listing on MSDN and noticed that there’s a Delete() method on SPList, so I tried using that.  Then, I found this article on how to create and delete lists uses the Delete() method of the SPWeb.Lists collection to delete a list.

So which one do you use?  I found that it’s better to use the Delete() method of SPWeb.Lists; using the Delete() method on the SPList sometimes seems not to delete the list properly.  So to delete a list use code like this:

SPWeb thisSite = SPContext.Current.Web;
SPList myCustomList = mySite.Lists["MyCustomList"];

I used this in a FeatureDeactivating event to remove a list that I created as part of a feature.

I like my features to clean themselves up properly when they get deactivated; and that includes deleting any lists that they might have created.  The disadvantage of this of course is that users might have put things into those lists and then when they deactivate the feature, all their data is gone, and they never get the chance to back out.  It would be great if there was a way to warn users what’s going to happen when you deactivate a feature, and give them the option to cancel.  SharePoint does pop up an extra page after you click on “deactivate”, but it’s just a generic page which doesn’t really give any detail about what’s going to happen.  I’m thinking of a way to allow the feature to give more detail to the end user.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: