Cleaning Up Redirect Sites in SharePoint Online

As our tenants evolve, we end up renaming sites and/or changing their URLs. When we change the URL of a site in the SharePoint Admin Center, the URL changes, just like we asked. SharePoint also leaves the old site in place, but converts it into a Redirect Site. So if we had a site at /sites/Procurement and we decide to change it to /sites/Purchasing (maybe due to a reorganization), people who have bookmarked the Procurement site will be redirected to the Purchasing site.

The Redirect Sites we end up with aren’t shown in the Active Sites listing. The only way we can figure out they are even there is to use PowerShell.

In most cases, we want that redirection to occur. But what about the cases where we mistakenly set up a site at the wrong URL? For example, maybe we create a site at /sites/HS by mistake, so we change it to /sites/HR. Later, we may have a new department called Highly Socialized. (Roll with it.) That /sites/HS URL is already “used up” by the Redirect Site. If we try to create a site with the /sites/HS URL, we’ll end up with /sites/HS2. SharePoint helpfully avoids a naming collision by adding the 2, and then a 3, then 4 – see if you can spot the pattern!

This becomes especially important if you’re creating sites with their URLs matching some sort of coding scheme. For example, for a property management company we work with, each Property has its own site with a URL using the Property Code. The property with a Property Code of 211 lives at /sites/211. By using that rigid connection to a property of the Property, we can guarantee unique URLs and keep them simple. Something like /sites/TheHilton-PhoenixEast just aren’t that fun to type!

If we mistakenly create the site for Property Code 211 at /sites/212 and then rename the URL to the correct /sites/211, we can get ourselves into trouble. What happens when we need to create a site for property 212? SharePoint will create the URL /sites/2122, which just isn’t right!

Therefore, we often want to clean out some of the Redirect Sites. Here’s the simple PowerShell to do this. Redirect Sites use the Site Template RedirectSite#0.

# Import modules
Import-Module PnP.PowerShell

$adminSiteUrl = "https://tenantName-admin.sharepoint.com"

Connect-PnPOnline -Url $adminSiteUrl -Interactive

$redirectSites = Get-PnPTenantSite -Template "RedirectSite#0"

########################################################################
# STOP HERE - Validate the redirect sites you actually want to remove. #
########################################################################

foreach ($site in $redirectSites) {
    Remove-PnPTenantSite -Url $site.Url -Force    
}

Note the big warning at line 10. Most likely, you won’t want to delete ALL the Redirect Sites. Once you’ve found them all, figure out what filter you might want to apply to $redirectSites before you run the foreach loop. Maybe you’ll do something like:

$redirectSites = Get-PnPTenantSite -Template "RedirectSite#0" | Where-Object { $_.Url -eq "https://tenantName.sharepoint.com/sites/212"

or

$redirectSites = Get-PnPTenantSite -Template "RedirectSite#0" | Where-Object { $_.Url -gt "https://tenantName.sharepoint.com/sites/211"

Good site hygiene is important, if only to protect your sanity. And use the URL renaming function thoughtfully. Don’t think of it as an easy way to clean up your mistakes, or your tenant may end up littered with Redirect Sites you don’t even know you have.

Resources

Manage site redirects – SharePoint in Microsoft 365 | Microsoft Docs

Similar Posts

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.