Women Race

Remove SharePoint Sites fastly

Howdy guys, sometimes we create ton of sites just for testing. A clean tenant is a must for an efficient management. Have you ever removed sites by hand? It took me at least 18 seconds to remove ONE site restless by hand. Come on, there MUST be a way to do it faster. In this article I want to show you how you can remove SharePoint sites fastly by using Out-GridView.

Step 1 – Connect to SharePoint Online with PowerShell

In order to remove SharePoint Sites fastly, we connect to our admin Site with PNP PowerShell. Basically we make use of cmdlets handled in this article:

Connect to SharePoint Online with PowerShell (sposcripts.com)

Connect-PnPOnline https://devmodernworkplace-admin.sharepoint.com/ -Interactive

Step 2 – Remove all sites, which you don’t need

After connecting, I basically make use of Out-GridView -PassThru, to pass my selected sites for deletion. If you are not familiar with Out-GridView check following article. It will help definitelly:

Get Items / Files interactively – SPO Scripts

Get-PnPTenantSite -IncludeOneDriveSites -Detailed  | Out-GridView -PassThru | ForEach-Object { 

    if ($_.Template -eq "GROUP#0") 
    {
        try
        {
            Remove-PnPMicrosoft365Group -Identity $_.GroupID 
            Write-Host "Removed M365 Group $($_.GroupID)" -ForegroundColor Green
        }
        catch
        {
            Write-Error "Could not remove M365 Group $($_.GroupID) $($Error[0].ErrorDetails.Message)"
        }
        
    }
    else
    {
        try
        {
            Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop
            Write-Host "Removed Site $($_.URL)" -ForegroundColor Green
        }
        catch
        {
            Write-Error "Could not remove Site $($Error[0].ErrorDetails.Message)"
        } 
    }
}

So what will happen now? A popup will show up, where you can select the sites, which you don’t want to use. As you can see, I marked 3 sites. After clicking okay, it will be removed, but you still we see them in the recycle bin – so no need for panic ;).

Choice of sites, which have to be deleted

If you want to remove the sites  without residue, you have to make use of following cmdlets. Sites, which belong to the a M365 Group will be put in recycle bin anyways.

Get-PnPTenantSite -IncludeOneDriveSites -Detailed  | Out-GridView -PassThru | ForEach-Object { 

    if ($_.Template -eq "GROUP#0") 
    {
        try
        {
            Remove-PnPMicrosoft365Group -Identity $_.GroupID 
            Write-Host "Removed M365 Group $($_.GroupID)" -ForegroundColor Green
        }
        catch
        {
            Write-Error "Could not remove M365 Group $($_.GroupID) $($Error[0].ErrorDetails.Message)"
        }
        
    }
    else
    {
        try
        {
            Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop -SkipRecycleBin
            Write-Host "Removed Site $($_.URL)" -ForegroundColor Green
        }
        catch
        {
            Write-Error "Could not remove Site $($Error[0].ErrorDetails.Message)"
        } 
    }
}

Thats it! The three sites I have marked, are deleted now. In my case even restless.

Screenshots of removal

Step 3 – Controll your Action

Controlling this, can be done by following cmdlet:

Get-PnPTenantSite -IncludeOneDriveSites  | Out-GridView -PassThru 

You might see, that there are still sites, which are related to M365 groups. This sites wil be removed by a job afterwards, so no need to worry.

Screenshot of controll action

BONUS 1: Wrapped all in functions

I wrapped all the stuff in functions, so you don’t have to do it and proceed to remove SharePoint Sites fastly.

Function Invoke-SPOSiteRemoval
{
    Param
    (
        $Site,
        [Switch]$SkipRecycleBin
    )

    if ($_.Template -eq "GROUP#0") 
    {
        try
        {
            Remove-PnPMicrosoft365Group -Identity $_.GroupID 
            Write-Host "Removed M365 Group $($_.GroupID)" -ForegroundColor Green
        }
        catch
        {
            Write-Error "Could not remove M365 Group $($_.GroupID) $($Error[0].ErrorDetails.Message)"
        }
        
    }
    else
    {
        try
        {
            if ($SkipRecycleBin)
            {
                Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop -SkipRecycleBin
            }
            else
            {
                Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop
            }
            
            Write-Host "Removed Site $($_.URL)" -ForegroundColor Green
        }
        catch
        {
            Write-Error "Could not remove Site $($Error[0].ErrorDetails.Message)"
        } 
    }

}

Get-PnPTenantSite -IncludeOneDriveSites -Detailed  | Out-GridView -PassThru | ForEach-Object { Invoke-SPOSiteRemoval -Site $_ -SkipRecycleBin }

BONUS 2: Ready-To-Use Script

$TenantAdminUrl = "https://devmodernworkplace-admin.sharepoint.com/"
Connect-PnPOnline $TenantAdminUrl -Interactive

Function Invoke-SPOSiteRemoval
{
    Param
    (
        $Site,
        [Switch]$SkipRecycleBin
    )

    if ($_.Template -eq "GROUP#0") 
    {
        try
        {
            Remove-PnPMicrosoft365Group -Identity $_.GroupID 
            Write-Host "Removed M365 Group $($_.GroupID)" -ForegroundColor Green
        }
        catch
        {
            Write-Error "Could not remove M365 Group $($_.GroupID) $($Error[0].ErrorDetails.Message)"
        }
        
    }
    else
    {
        try
        {
            if ($SkipRecycleBin)
            {
                Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop -SkipRecycleBin
            }
            else
            {
                Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop
            }
            
            Write-Host "Removed Site $($_.URL)" -ForegroundColor Green
        }
        catch
        {
            Write-Error "Could not remove Site $($Error[0].ErrorDetails.Message)"
        } 
    }

}

Get-PnPTenantSite -IncludeOneDriveSites -Detailed  | Out-GridView -PassThru | ForEach-Object { Invoke-SPOSiteRemoval -Site $_ -SkipRecycleBin }

#Controlling
Get-PnPTenantSite -IncludeOneDriveSites -Detailed  | Out-GridView -PassThru

Conclusio

Using Out-GridView saves you a ton of time, when you want to remove sites in SharePoint fastly.

Further links

Microsoft Docs for the removal cmdlet Remove-PnPTenantSite (PnP.Powershell) | Microsoft Docs

Microsoft Docs for Out-Gridview Out-GridView (Microsoft.PowerShell.Utility) – PowerShell | Microsoft Docs


This Post Has One Comment

Leave a Reply