Delete custom site scripts in SharePoint online

Delete custom site scripts in SharePoint online

In SharePoint online, you can use Site templates (previously known as “Site designs”) and Site scripts to provide reusable site columns, content types, lists, themes, site navigation layouts, or custom actions so that your users can quickly build new SharePoint sites with the features they need. SharePoint custom site scripts can help you to add custom branding, theming, or automation like setting a site logo, activating a site feature, etc. to your SharePoint online site.

However, over time, these custom site scripts can become outdated or unnecessary and ends up just hanging around in your SharePoint tenant for a long time even though you no longer need them. In this blog, we will discuss how to delete the custom SharePoint site scripts using PowerShell “scripts” (pun intended).

Using SharePoint Online PowerShell

You can run below SharePoint online PowerShell script from SharePoint Online Management Shell to delete the custom site scripts in your tenant:

# SharePoint online admin site URL
$siteUrl = "https://contoso-admin.sharepoint.com/"

# Connect to SharePoint online admin center
Connect-SPOService -Url $siteUrl

# Get all site scripts from the current tenant
$siteScripts = Get-SPOSiteScript

# List of custom site scripts to exclude from deletion
$keepThese = "Base Site Settings", "English Region", "Standard Site Columns", "Standard Libraries"
$siteScripts = $siteScripts | Where-Object { -not ($keepThese -contains $_.Title)}

if ($siteScripts.Count -eq 0) { break }

$siteScripts | Format-Table Title, SiteScriptIds, Description

Read-Host -Prompt "Press Enter to start deleting (CTRL + C to exit)"
$progress = 0
$total = $siteScripts.Count

foreach ($siteScript in $siteScripts)
{
    $progress++
    Write-Host $progress / $total":" $siteScript.Title

    # Delete custom site script
    Remove-SPOSiteScript -Identity $siteScript.Id
}

# Disconnect SharePoint online connection
Disconnect-SPOService

Using PnP PowerShell

Use below PnP PowerShell script to delete the custom site scripts from SharePoint online tenant:

# SharePoint online admin site URL
$siteUrl = "https://contoso-admin.sharepoint.com/"

# Connect to SharePoint online admin center
Connect-PnPOnline -Url $siteUrl -Interactive

# Get all site scripts from the current tenant
$siteScripts = Get-PnPSiteScript

# List of custom site scripts to exclude from deletion
$keepThese = "Base Site Settings", "English Region", "Standard Site Columns", "Standard Libraries"
$siteScripts = $siteScripts | Where-Object { -not ($keepThese -contains $_.Title)}

if ($siteScripts.Count -eq 0) { break }

$siteScripts | Format-Table Title, SiteScriptIds, Description

Read-Host -Prompt "Press Enter to start deleting (CTRL + C to exit)"
$progress = 0
$total = $siteScripts.Count

foreach ($siteScript in $siteScripts)
{
    $progress++
    Write-Host $progress / $total":" $siteScript.Title

    # Delete custom site script
    Remove-PnPSiteScript -Identity $siteScript.Id
}

# Disconnect SharePoint online connection
Disconnect-PnPOnline

Using CLI for Microsoft 365

Use below CLI for Microsoft 365 script to remove custom SharePoint site scripts from your tenant:

# Get credentials to connect to SharePoint online tenant
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
    m365 login
}

# Get all site scripts from the current tenant
$siteScripts = m365 spo sitescript list | ConvertFrom-Json

# List of custom site scripts to exclude from deletion
$keepThese = "Base Site Settings", "English Region", "Standard Site Columns", "Standard Libraries"
$siteScripts = $siteScripts | Where-Object { -not ($keepThese -contains $_.Title)}

if ($siteScripts.Count -eq 0) { break }

$siteScripts | Format-Table Title, SiteScriptIds, Description

Read-Host -Prompt "Press Enter to start deleting (CTRL + C to exit)"
$progress = 0
$total = $siteScripts.Count

foreach ($siteScript in $siteScripts)
{
    $progress++
    Write-Host $progress / $total":" $siteScript.Title

    # Delete custom site script
    m365 spo sitescript remove --id $siteScript.Id
}

# Disconnect SharePoint online connection
m365 logout

These PowerShell scripts are also available on PnP Script Samples site at: Delete custom SharePoint site scripts.

Learn more