none
Programmatically Delete SP Online subsites (move to recycle NOT permanently delete)

    Question

  • I would like to programmatically (CSOM/PS/etc) be able to delete subsite(s) within a SPO site collection. However, I want the deleted subsite to be moved to the recycling bin and NOT permanently deleted. 

    For example OnPrem
    Remove-SPWeb $subsite.url -Confirm:$false -Recycle:$true

    Is there a way to do this with an online subsite?


    TJK

    Thursday, June 9, 2016 3:04 PM

All replies

  •                                    

    Hi TJK,

    Use below JSOM code to delete subsite, the below is the sample code to delete the site using sharepoint. you can modify the code as your need

    function deleteSite() {

        var spContext = SP.ClientContext.get_current();
        var clientContext = new SP.AppContextSite(spContext, siteCollURL);
        var rootweb = clientContext.get_site().get_rootWeb();
        var webs = rootweb.get_webs();
        spContext.load(webs);
        spContext.executeQueryAsync(
            function () {
                for (var i = 0; i < webs.get_count() ; i++) {
                    var web = webs.getItemAtIndex(i);
                    if (web.get_url() == projectURL) {
                        web.deleteObject();
                        break;
                    }
                }
                rootweb.update();
                spContext.load(rootweb);
                spContext.executeQueryAsync(function () {

                    console.log("Site has been deleted.");
                }, function (sender, args) {

                    console.log('Site deletion failed : ' + args.get_message() + '\n' + args.get_stackTrace());
                });
                return;
            },
            function (sender, args) {           
                console.log('Site deletion failed : ' + args.get_message() + '\n' + args.get_stackTrace());
            });
    }


    Thanks, Kaviya (Please remember to click “Mark as Answer” on the post that helps you) Blog:


    • Edited by KaviyaMVP Tuesday, June 14, 2016 5:26 AM
    • Proposed as answer by KaviyaMVP Thursday, August 25, 2016 8:47 AM
    • Unproposed as answer by KaviyaMVP Thursday, August 25, 2016 8:47 AM
    • Proposed as answer by KaviyaMVP Thursday, August 25, 2016 8:48 AM
    Tuesday, June 14, 2016 5:25 AM