Monday, May 14, 2018

SharePoint TIP: quickly and recoverably remove sites in bulk using a single line of PowerShell

To quickly and recoverably remove sites in bulk, using piping.  For example, if a content database has hundreds of MySite sites that you do not want to upgrade, you can quickly remove them from the content database(s) like so:
Get-SPSite -WebApplication "[webappurl]" -LimitAll | Where-Object {$_.URL -Like "*MySites*"} | Remove-SPSite -GradualDelete -Confirm:$False
That's it. Execute this in an elevated SharePoint Management Shell (SMS). The -GradualDelete parameter moves the deleted sites into the SPDeletedSite collection in the content database, and the -Confirm:$False parameter ensures that you are not prompted hundreds of times for each site to be removed.

References

  • I use this when testing content databases to be migrated.  After making a copy of the production version backup, I sometimes need to remove legacy sites and want to do this in bulk, such as removing old MySite sites.  You can even do this in parts by filtering for MySite sites starting with "a", or "b", etc, like "*MySites/a* or "*MySites/b*".  I then do a quick check by interrogating the SPDeletedSites collection to ensure that what I expected to delete was in fact deleted.