How to delete subsites in SharePoint using Powershell

To delete a site with subsites in SharePoint, we have to delete all child subsites and then delete the parent site. What if you have large number of subsites?

Using the following Powershell to delete all subsites

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Site URL
#Custom Function to Delete subsite recursively
function Remove-SPWebRecursively([Microsoft.SharePoint.SPWeb] $web)
{
    Write-host "Removing Web $($web.Url)..."
    
    $ChildWebsColl = $web.webs
    
    foreach($ChildWeb in $ChildWebsColl)
    {
        #Call the function recursively
        Remove-SPWebRecursively($ChildWeb)
        $ChildWeb.Dispose()
    }
    
    #Remove the web   
    Remove-SPWeb $web -Confirm:$false
}
#Get the paretn web
$ParentWeb = Get-SPWeb $ParentWebURL
#Call the function to remove subsite
Remove-SPWebRecursively $ParentWeb

About Terri

System Administrator @Netpower Datacenter

Posted on 24.09.2015, in SharePoint, Technical Articles and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: