Delete custom SharePoint site scripts
Remove Site scripts that ends up just hanging around in your tenant. Use the scripts below to get rid of them
Summary
Site designs and especially site scripts can be something that ends up just hanging around in your tenant for a long time even though you no longer need them for anything. Use the scripts below to get rid of them. You might also find some site scripts that are not linked to any site design and hence never get executed!
Connect-SPOService "https://contoso-admin.sharepoint.com"
$keepThese = "Base Site Settings", "English Region", "Standard Site Columns", "Standard Libraries"
$siteScripts = Get-SPOSiteScript
$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
Remove-SPOSiteScript $siteScript.Id
}
# SharePoint online admin site url
$siteUrl = "https://contoso-admin.sharepoint.com/"
# Connect to SharePoint online site
Connect-PnPOnline -Url $siteUrl -Interactive
$keepThese = "Base Site Settings", "English Region", "Standard Site Columns", "Standard Libraries"
# Get all site scripts from the current tenant
$siteScripts = Get-PnPSiteScript
$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
Remove-PnPSiteScript -Identity $siteScript.Id
}
# Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
m365 login
}
$keepThese = "Base Site Settings", "English Region", "Standard Site Columns", "Standard Libraries"
# Get all site scripts from the current tenant
$siteScripts = m365 spo sitescript list | ConvertFrom-Json
$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
m365 spo sitescript remove --id $siteScript.Id
}
Contributors
| Author(s) |
|---|
| Paul Bullock |
| Ganesh Sanap |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
Tool documentation & references
- Introduction to the SharePoint Online Management Shell
Check out the SPO Management Shell documentation site to get started and for the reference to the cmdlets.
- Want to learn more about PnP PowerShell and the cmdlets
Check out the PnP PowerShell site to get started and for the reference to the cmdlets.
- Want to learn more about CLI for Microsoft 365 and the commands
Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.