0

(FIRST POST - I will do my best to summerize, forgive me ahead of time)

I have a SharePoint site with a list of sites to be deleted, problem is, some of the sites to be deleted have subsites that need to be deleted before the deletion can occure. I have a basic Powershell script that will delete the site as long as there are no subsites.

I am trying to come up with either a method to delete the subsites if the listed site to be deleted has subs or a way to catch the ones that have an error to enter data into a column on the same list or output a file AND to continue deleting the sites without subs.

Here is what I have so far:

    #========================================================================#
    #                       Delete confirmed sites                           #
    #========================================================================#


function deleteSites_old ($url, $listName) {
    
    $web = get-SPWeb $url
    $list = $web.Lists[$listname]
    
    $items = $list.items | Where-Object {($_['Status'] -eq 'Delete')}
    Write-host "List $($list.title) has $($items.count) entries to be deleted" 


    
    foreach ($item in $items) {
        
        $siteURL = $item["URL"]
        $siteOwner = $itme["SiteOwner"]

        write-host $siteURL $siteOwner

        try {
            write-host "Site deletion"
            #Remove-SPWeb -Identity $siteURL -Recycle
        }
        catch [Microsoft.SharePoint.Powershell.SPCmdRemoveWeb] {
            "There was a problem deleting web site $($siteURL)"
            
        }

        #Remove-SPWeb -Identity $siteURL -Recycle

        #$ListItem["Status"] = "DELETED"
        #$ListItem.update()

    }


    #DELETES THE INSTANCE
    $web.Dispose()  

}


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue


$Site = '[URL]'
$list = "[List Name]"
 
deleteSites_old $Site $List

I am still testing so I have commented out as I continue. Thanks ahead of time, I did search the forum first but only found questions related to SPOnline or SP13 (with $ChildSites but couldn't get it to work for my environment)

Riley
  • 1
  • 2
  • Are you sure that you have to delete the subsites first? The [documentation](https://learn.microsoft.com/en-us/powershell/module/sharepoint-server/remove-spweb?view=sharepoint-ps) for `Remove-SPWeb` says "Deleting the top level Web site of a site collection causes the entire site collection to be removed." – TheMadTechnician Aug 11 '21 at 22:07
  • Remove-SPweb will remove a top level site and it's subsites but if I want to only to delete a site within that collection it gives me an error because of the subsites. I am in the process of using PnP cmdlets to see if that will allow deletion of a site with subsites – Riley Aug 19 '21 at 14:49

0 Answers0