Using the App Model for deleting Site Collections and Sites

Using the App Model for deleting Site Collections and Sites

Earlier I posted about Using the App Model for deploying Site Collections and Sites. In those examples we explored the options we have for creating new sub sites and site collections. However if you use the App Model for structured and repeatable deployments you will also take care of the cleanup or rollback is something fails. If there are problems in the out of the box templates that you are using SharePoint will take care of the rollback process for you, but if somewhere in the process you facilitate fails you will also have to take care of cleanup or rollbacks. So in order to support structured and repeatable deployments you should have logic for both the creation and deletion of artifacts.

Using the App Model for deleting Site Collections

Deleting a Site Collection almost works the same way as creating it and will differ only in one major way. If you delete a Site Collection in the UI or through the App Model it will end up in the recycle bin. Then if you are creating new Site Collections through the UI you have the option to overwrite any site with the same name in the recycle bin. However if you are creating new Site Collections through the App Model you do not have this option yet. Depending on your requirements you will either have to write logic to delete it from the recycle bin as well, or accept that the Site Collection is still present, and that you can no longer create a new Site Collection with that specific URL. Items in the recycle bin will stay there for 30 days before they get deleted.

For deleting the Site Collection it self you can resort to a SpoOperation of type RemoveSite, assuming you have a tenant object. If that is unclear please read Using the App Model for deploying Site Collections and Sites. To remove a Site Collection all you need pass on is a URL and await completion.

SpoOperation removeOp = tenant.RemoveSite(String.Format("https://{0}/sites/SPSNL14-2", siteUri.Host));

adminContext.Load(removeOp, i => i.IsComplete, i => i.PollingInterval);
adminContext.ExecuteQuery();

while (!removeOp.IsComplete) {
    System.Threading.Thread.Sleep(removeOp.PollingInterval);
    removeOp.RefreshLoad();

    if (!removeOp.IsComplete) {
        try {
            adminContext.ExecuteQuery();
        }
        catch (WebException) {
            // ctx connection get's closed after action completed...calling ExecuteQuery again can return an error that we can ignore
        }
    }
}

Once the Site Colleciton is deleted and the operation is done you can also create a new SpoOperation of type RemoveDeletedSite to remove the Site Collection from the recycle bin.

SpoOperation deleteOp = tenant.RemoveDeletedSite(String.Format("https://{0}/sites/SPSNL14-2", siteUri.Host));

adminContext.Load(deleteOp, i => i.IsComplete, i => i.PollingInterval);
adminContext.ExecuteQuery();

while (!deleteOp.IsComplete) {
    System.Threading.Thread.Sleep(deleteOp.PollingInterval);
    deleteOp.RefreshLoad();

    if (!deleteOp.IsComplete) {
        try {
            adminContext.ExecuteQuery();
        }
        catch (WebException) {
            // ctx connection get's closed after action completed...calling ExecuteQuery again can return an error that we can ignore
        }
    }
}

As both operations will be done quite quickly you can choose to ignore the PollingInterval and set it to a second rather then the 15 seconds that is the default value. However since I rather not stress my Office 365 tenant I kept it to the default PollingInterval, though this will slow things down a little.

Using the App Model for deleting Sites

As with creating sub sites the sample is equally easier. All you have to do is to retrieve the site that you would like to delete and you are ready to go

Web web = clientContext.Site.OpenWeb("Sessions");
web.DeleteObject();
clientContext.ExecuteQuery(); 

You can download the Sample App for SharePoint (227KB, ZIP) that contains both the code from Using the App Model for deploying Site Collections and Sites and the samples explained in this post.

Leave a Reply