Okay – so I’m trying to test some import issues with lists and decide to export an existing list that has documents set the way I need them. I run my import test and realize I’ve got more work to do so I go to delete the list via the browser. Hmm… the delete option isn’t there – no problem I say – there’s a forcedeletelist command that I just blogged about last week so I’ll use that. But what’s this – I ran the command and I got this error: “The requested delete operation could not be performed. (Exception from HRESULT:0x800720CE)”.

Okay – what the heck! What’s the point of a “FORCE"deletelist command if it doesn’t FORCE the deletion of the list. So I did some testing and I discovered that the reason it couldn’t delete it was because there was a property called “AllowDeletion” which was set to false (my list was the “Documents” list which is part of the publishing feature so it’s set to not allow deletion). I checked this property out and it turns out that it’s a really simple property with public getter and setters (the later being unusual in my experience – I usually have to use reflection to hack the objects). So I set the property to true and then tried a simple SPList.Delete() and it worked like a charm.

So what did I conclude – forcedeletelist is misnamed and should be abandoned – I’ve created a new command called simply gl-deletelist which allows you to pass a -force parameter in to actually force the deletion of the list. Now granted – there may be cases in which mine still fails but at least it will handle the AllowDeletion setting! Here’s the code:

 1internal static void Delete(SPList list, bool force, string url)
 2{
 3    if (list == null)
 4        throw new SPException("List not found.");
 5
 6    if (!list.AllowDeletion && force)
 7    {
 8        list.AllowDeletion = true;
 9        list.Update();
10    }
11    else if (!list.AllowDeletion)
12        throw new SPException("List cannot be deleted.  Try using the '-force' parameter to force the delete.");
13
14    try
15    {
16        list.Delete();
17    }
18    catch (Exception)
19    {
20        if (force)
21        {
22            using (SPSite site = new SPSite(url))
23            {
24                Utilities.RunStsAdmOperation(
25                string.Format(" -o forcedeletelist -url \"{0}\"",
26                site.MakeFullUrl(list.RootFolder.ServerRelativeUrl)), false);
27            }
28        }
29    }
30}

The syntax of the command can be seen below:

C:\>stsadm -help gl-deletelist

stsadm -o gl-deletelist

Deletes a list.

Parameters:
        -url <list view URL>
        [-force]

Here’s an example of how to delete a list passing in the force parameter:

stsadm –o gl-deletelist -url "http://intranet/Documents/Forms/AllItems.aspx" -force