Recursively Delete a SharePoint Web Site

By James|07/31/2011|,

Let’s say you have a SharePoint web site at http://your_portal/stuff/trash .  Your boss says “Get rid of that trash web site, I don’t want it anymore”.  So, you bring up the web site, open Site Actions and select Site Settings.  Under Site Administration, you select “Delete this site”.  After confirming that you selected the correct site to delete, you smack the “Delete” key and mutter “Later!” under your breath.  But, before you can smirk in satisfaction, you receive a message that the site cannot be deleted because it contains sub sites.  Your smirk turns into a scowl, and a few choice expletives come to mind.

You try the STSADM command line tool.  You fire up a command prompt, punch in “stsadm.exe -o deleteweb -url http://your_portal/stuff/trash and pound the enter key.  Again, the delete fails because the web site contains sub sites.

Whether using the web GUI or STSADM, SharePoint will only let you delete a web site that does not contain any sub sites.  So, let’s say your trash web site looks like this:

your_portal/stuff/trash
your_portal/stuff/trash/more_trash
your_portal/stuff/trash/more_trash/even_more_trash
your_portal/stuff/trash/more_trash/even_more_trash/still_more_trash
your_portal/stuff/trash/surprise
your_portal/stuff/trash/surprise/lions_tigers_and_bears
your_portal/stuff/trash/surprise/lions_tigers_and_bears/oh_my

First, you have to figure out what all the sub sites are.  Then, you have to navigate to the deepest web site and work your way backwards to the top-level site, deleting as you go.  In this example, you would have to navigate to 7 web sites and individually delete each one.  Now let’s imagine that the site had 100 sub sites.  Ouch!

The following PowerShell script will recursively delete a web site and sub sites below it.  It will NOT delete a root web site.  Delete the site collection if you want to do that.

# Title: DeleteWeb
# Version: 1.0 30JUL11
# Author: James Sanders
# Purpose: Delete a SharePoint web site and all subwebs

# Command line parameters
Param([string]$URL, [switch]$Verbose, [switch]$Help, [switch]$h)

#############################
# Internal script functions #
#############################

# Verify URL exists
Function VerifyURL([string]$URL) {
  If (!($URL)) {Throw 'Parameter -URL is missing!'}
  Try {
    $Request = [System.Net.WebRequest]::Create($URL)
    $Request.Method = 'HEAD'
    $Response = $Request.GetResponse()
    $Status = $Response.StatusCode
    $Valid = ($Status -eq 'OK')
    $Response.Close()
  }
  Catch [System.Exception] {$Valid = $False}
  Return $Valid
}

# Retrieve a SharePoint web
Function Get-SPWeb ([string]$URL) {
  If (!($URL)) {Throw 'Parameter -URL is missing!'}
  $Site = New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList $URL
  Return $Site.OpenWeb()
}

# Enumerate SharePoint web site to include all subwebs
Function EnumerateWebs ([Microsoft.SharePoint.SPWeb]$Web, [array]$Webs) {
  If ($Web.Webs.Count -gt 0) {
    $Webs = $Webs + $Web.Url
    ForEach ($SubWeb In $Web.Webs) {
      EnumerateWebs $SubWeb
    }
  }
  Else {
    $Webs = $Webs + $Web.Url
  }
  Return $Webs
}

# Show help
Function ShowHelp() {
$HelpText =@"

DeleteWeb
Delete a SharePoint web site and all sub webs

PARAMETERS:
URL URL of web site to delete
-verbose Displays additional web site information such as sub webs
-help Displays this help information
-h Displays this help information

"@
$HelpText
}

# Show help if requested and exit
If($Help -Or $h) {ShowHelp; Exit}

# Verify URL was passed
If (!($URL)) {ShowHelp; Exit}

# Show script header
"`nDeleteWeb"
"Delete a SharePoint web site and all sub webs`n"

# Verify URL is valid
If (!(VerifyURL $URL)) {"URL ""$URL"" is invalid."; Exit}

# Load SharePoint PowerShell extensions
"- Loading SharePoint extensions"
[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

"- Retrieving web site details"
$Web = Get-SPWeb $URL
If ($Web.IsRootWeb) {"`nCannot delete root web. To delete this web site, delete the site collection.`n";Exit}
$Webs = EnumerateWebs $Web|Sort-Object
$Web.Dispose()

"`nDETAILS"
"Web Site: $URL"
"Sub Webs: $($Webs.Count)"

If ($Verbose) {
  ""
  ForEach ($WebURL In $Webs) {
  "$WebURL"
  }
}

"`nCONFIRM"
$Result = $Null
While ($Result -ne "Y" -and $Result -ne "N") {
  $Result = Read-Host "Delete Site [Y/N]: "
}

# Exit if user aborted
If ($Result -eq "N") {Exit}

# Delete web site
""
$Webs = $Webs|Sort-Object -Descending
ForEach ($WebURL In $Webs) {
  "Deleting web $WebURL"
  $Web = Get-SPWeb $WebURL
  $Web.Delete()
  $Web.Dispose()
}
"`nDone!`n"

 

Copyright 2011 - 2024 The Lazy IT Admin | All Rights Reserved
menu-circlecross-circle linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram