Wednesday, 13 October 2010

Delete multiple SharePoint sites using PowerShell

It can be very time consuming in the SharePoint browser UI to delete a large structure of sub-sites below a particular site. This is because you are unable to delete a site that contains other sub-sites without deleting those sub-sites first, which in turn may contain other sub-sites!

The PowerShell script below allows you to choose a start site (i.e., the site from which you want to delete all sub-sites), walks through each sub-site and deletes it, starting from sites at the end of the hierarchy and working backwards until it returns back to the start site, which it will not delete.

First, run the PowerShell script below – Note: Doing this will not start deleting sites as you have to call the functions in the script with a separate command (see below).

function Delete-SPSubSites ($StartSite)
    Start-SPAssignment –Global
    #Get the starting site from which to delete all sub-sites
    $web = Get-SPWeb $StartSite
    #Run function to get all sub-sites below the start site
    Get-SPSubWebs -StartWeb $web
    Stop-SPAssignment –Global

function Get-SPSubWebs ($StartWeb)
    #Get all sub-sites below the start site
    $webs = $StartWeb.GetSubwebsForCurrentUser()
    #Run this routine if there are sub-sites present
    if ($webs.Count -gt 0)
        #Walk through each sub-site
        foreach ($nextWeb in $webs)
            #Check for sub-sites underneath this site
            Get-SPSubWebs -StartWeb $nextWeb
            #Run function to delete the site
            Delete-SPSubWeb -DeleteWeb $nextWeb

function Delete-SPSubWeb ($DeleteWeb)
    #Delete the site specified in the $DeleteWeb variable
    write-host "Deleting site"$DeleteWeb.Title"at"$DeleteWeb.Url

Once you have run the script, you can use it to delete sub-sites by typing the following command in the PowerShell console:

Delete-SPSubSites -StartSite http://portal/team

The example above will delete all sub-sites below http://portal/team, but will not delete the http://portal/team site itself.


In case it’s not obvious (!), be very careful with the use of this script. Take a backup of your site collection first, and run on a development server before using this script in a live environment to ensure you delete the correct sites.


  1. Can you run this from the Sharepoint Management Console? Powershell isn't recognizing the function Delete-SPSubSites even though I can successfully execute the script before trying to call it using the final instruction of your post. Any thoughts?

    Error Received:

    The term 'Delete-SPSubSites' is not recognized as the name of a cmdlet, functio
    n, script file, or operable program. Check the spelling of the name, or if a pa
    th was included, verify that the path is correct and try again.
    At line:1 char:18
    + Delete-SPSubSites <<<< -StartSite http://siteurl + CategoryInfo : ObjectNotFound: (Delete-SPSubSites:String) [], C
    + FullyQualifiedErrorId : CommandNotFoundException

  2. You have to put the script in the standard library or you need to prefix the script with the directory name as in the picture

  3. Online gaming is a hobby of many people. Game Run Unblockedversions as Run 3 Unblocked or Run 4 Unblocked is a very interesting game to control the bear run or jump to the safe landing area falls. risks with such racing game Car Racing Games or interesting as the game Donkey Kong Unblocked or Car Racing Games wrap pulled a lot of players. Good luck!

  4. También puede visitar una tienda de ladrillo y mortero de Michael Kors o su sitio web y comprar directamente un bolso Michael Kors desde allí. Usar un bolso de Michael Kors les permite a los demás reconocer que el habitante urbano educado toma la moda realmente con seriedad. Los bolsos de hombro son particularmente refinados y elegantes.

    {Bolsas Michael Kors Precios | Bolsos Michael Kors Outlet | Michael Kors Rebajas}

    En vacker konstnärlig skapelse av vävt läder, som ger ett skalskaligt utseende - liknar en snakeskin eller fiskhud, linjer utsidan av påsen. Läderens bältros är små läderringar. Det finns också gyllene accenter på väskan. Slutresultatet är svagt liknar kedjepost.

    {Michael Kors Rea | Michael Kors Väska Rea | Michael Kors Plånbok}