GitHub Icon Image

Delete custom SharePoint site scripts

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!

Warning

Please be aware this script contains a command that will remove or delete an artifact, ensure you test and understand the implications of running the script.

  • CLI for Microsoft 365 with PowerShell
  • Microsoft 365 CLI with Bash
  • SPO Management Shell
$sparksjoy = "Project Site", "Issues List"
$siteScripts = m365 spo sitescript list -o json | ConvertFrom-Json
$siteScripts = $siteScripts | where {  -not ($sparksjoy -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 -i $siteScript.Id --confirm
}

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

#!/bin/bash

# requires jq: https://stedolan.github.io/jq/

sparksjoy=("Project Site"  "Issues List")
sitesscriptstoremove=()
while read script; do
 scriptTitle=$(echo ${script} | jq -r '.Title')
  exists=true
  for keep in "${sparksjoy[@]}"; do
    if [ "$keep" == "$scriptTitle" ] ; then
      exists=false
      break
    fi
  done
  if [ "$exists" = true ]; then
    echo $scriptTitle
    sitesscriptstoremove+=("$script")
  fi

done < <(m365 spo sitescript list -o json | jq -c '.[]')

if [ ${#sitesscriptstoremove[@]} = 0 ]; then
  exit 1
fi

echo "Press Enter to start deleting (CTRL + C to exit)"
read foo

for script in "${sitesscriptstoremove[@]}"; do
  scriptTitle=$(echo ${script} | jq -r '.Title')
  scriptId=$(echo ${script} | jq -r '.Id')
  echo "Deleting Site script..."  $scriptTitle
  m365 spo sitescript remove --id $scriptId --confirm
done

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

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
}

Check out the SPO Management Shell to learn more at: Introduction SharePoint Online Management Shell | Microsoft Docs

Source Credit

Sample first appeared on Delete custom SharePoint site scripts | CLI for Microsoft 365

Contributors

Author(s)
Laura Kokkarinen
Paul Bullock

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.

Back to top Script Samples
Generated by DocFX with Material UI