Make your own command for deleting SharePoint web site including all sub webs

MK .NET UG

MK .NET UG
Macedonian .NET User Group

Software Architecture and Related Concerns

If you think good architecture is expensive, try bad architecture.

Make your own command for deleting SharePoint web site including all sub webs

  • Comments 3

If you try to use sharepoint administration tool stsadm.exe to delete website you will probably notice that deleting website with this command is not possible if web contains sub-webs. This is highly frustrating especially if you have large structure of web/sub-webs for deleting.

Simple recursive iteration thru all subwebs will do the trick. Gary Lapointe published one solution for this problem here. This solution extends existing stsadm.exe tool with new command for recursive deleting. The drawback in this implementation is that all webs in a given site collection will be deleted.

Following code will delete ONLY specified website including all sub-webs.

Simple and useful.

 

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;

namespace deleteweb
{
    class Program
    {

        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                string url = args[0];
                SPSite site = null;

                //Open site collection
                try
                {
                    site = new SPSite(url);
                    if (site == null)
                    {
                        Console.WriteLine("Site cannot be opened");
                        return;
                    }
                }
                catch
                {
                    Console.WriteLine("Site cannot be opened");
                    return;
                }

                //Find site-relative url for web
                string weburl = url.Replace(site.Url, string.Empty).TrimStart(Convert.ToChar("/"));
                SPWeb web = site.OpenWeb(weburl);
                DeleteWeb(web);
            }
        }

        private static void DeleteWeb(SPWeb web)
        {
            try
            {
                foreach (SPWeb subweb in web.Webs)
                {
                    DeleteWeb(subweb);
                }
                web.Delete();
                Console.WriteLine(string.Format("{0} DELETED", web.Url));
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine(string.Format("{0} NOT FOUND", web.Url));
            }
            catch
            {
                Console.WriteLine(string.Format("{0} CANNOT BE DELETED", web.Url));
            }
        }

    }
}

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Handy command, I will definitely put it in my toolbox. Tnx

  • Just an FYI - the gl-deleteweb command does not delete all webs withing a site collection - only the specified web and it's child webs (in fact it will throw an exception if you pass in the root web of a site collection).

  • Hello to all :) I can’t understand how to add your site in my rss reader. Help me, please