Tuesday, 18 September 2012

Programmatically How to delete all web applications other than central administration




In previous post shown how to get all available web applications.
Here the code snippet for delete all web applications other than central administration

Method 1:


            SPWebApplicationCollection webapps = SPWebService.ContentService.WebApplications;
            foreach (SPWebApplication webapp in webapps)
            {
                foreach (SPAlternateUrl url in webapp.AlternateUrls)
                {
                        foreach (SPContentDatabase db in webapp.ContentDatabases)
                        {
                            db.Unprovision();
                        }
                        webapp.UnprovisionGlobally();
                        webapp.Delete();
                }
            }


Method 2:

If you dont want to delete some of required web application then specify those web application       names here otherwise comment if condition. Here i dont want to delete http://ukreddy:6969,http://ukreddy:80/ and http://ukreddy:2010 web application

            SPWebApplicationCollection webapps = SPWebService.ContentService.WebApplications;
            foreach (SPWebApplication webapp in webapps)
            {
                foreach (SPAlternateUrl url in webapp.AlternateUrls)
                {
                    if (url.Uri.ToString() != "http://ukreddy:6969/" && url.Uri.ToString() != "http://ukreddy/" && url.Uri.ToString() != "http://ukreddy:2010/")
                    {
                        foreach (SPContentDatabase db in webapp.ContentDatabases)
                        {
                            db.Unprovision();
                        }
                        webapp.UnprovisionGlobally();
                        webapp.Delete();
                    }
                }
            }

No comments:

Post a Comment