Skip to content

Automating SharePoint Site Deletion and Retention Policy Updates with PowerShell

Automating SharePoint Site Deletion and Retention Policy Updates

Section titled “Automating SharePoint Site Deletion and Retention Policy Updates”

Managing SharePoint Online sites and enforcing retention policies are crucial tasks for IT administrators, especially in large organizations with numerous SharePoint sites and Microsoft 365 Groups. This PowerShell script automates the process of excluding sites from a retention policy and permanently deleting SharePoint sites along with their associated Microsoft 365 Groups.


This PowerShell script leverages Microsoft Graph, PnP PowerShell, and Microsoft 365 Compliance Center cmdlets to automate both retention policy management and the cleanup of SharePoint sites.

FunctionDescriptionBusiness Value
Retention Policy ManagementExcludes sites from retention policiesEnables safe site deletion
Site CleanupPermanently deletes SharePoint sitesFrees up resources
Group ManagementRemoves associated Microsoft 365 GroupsMaintains clean environment
AutomationBatch processing of multiple sitesSaves administrative time

Before running the script, ensure you have the following:

RequirementDescriptionPurpose
Administrative AccessMicrosoft 365 Compliance CenterManage retention policies
SharePoint Admin RightsSharePoint Online administrationSite deletion capabilities
PnP PowerShellInstalled and configuredSharePoint management
Compliance PolicyExisting retention policyPolicy exclusion management

1. Connect to Microsoft 365 Compliance Center

Section titled “1. Connect to Microsoft 365 Compliance Center”

The script establishes a session with the Microsoft 365 Compliance Center using the Connect-IPPSSession cmdlet.

It imports a CSV file containing URLs of the SharePoint sites that need to be excluded from the retention policy.

Terminal window
$csv = Import-Csv -Path C:\Path\To\Your\CSVFile.csv
$SitesURL = $csv | Select-Object -ExpandProperty url

The script retrieves the existing retention policy by name and then adds the SharePoint site URLs to the exclusion list.

Terminal window
$Policy = Get-RetentionCompliancePolicy -Identity $PolicyName
If ($Policy) {
Set-RetentionCompliancePolicy -AddSharePointLocationException $SitesURL -Identity $Policy.Name
}

This ensures that the specified SharePoint sites are no longer subject to the retention policy, allowing them to be deleted safely.

The script uses a try/catch block to handle any errors that occur during the process, ensuring that administrators are informed if the policy update fails.


SharePoint Site and Group Deletion Process

Section titled “SharePoint Site and Group Deletion Process”

After excluding the SharePoint sites from the retention policy, the next step is to permanently delete the sites along with their corresponding Microsoft 365 Groups.

The script connects to the SharePoint Online admin center using Connect-PnPOnline, which allows it to manage SharePoint sites programmatically.

Terminal window
Connect-PnPOnline -Url $AdminCenterURL -Interactive

The script loops through the list of SharePoint sites and performs the following actions:

ActionCommandResult
Retrieve SiteGet-PnPTenantSiteGets site information
Delete SiteRemove-PnPTenantSite -Url $SiteUrl -ForcePermanent deletion
Delete GroupRemove-PnPMicrosoft365Group -Identity $Site.GroupIdRemoves associated group
Terminal window
Remove-PnPTenantSite -Url $SiteUrl -Force
Remove-PnPMicrosoft365Group -Identity $Site.GroupId

The script includes comprehensive error handling to log any issues encountered during the site or group deletion process, allowing administrators to troubleshoot problems quickly.


Best PracticeDescriptionImplementation
Test EnvironmentVerify script functionality before productionReduce deployment risks
Data BackupEnsure critical data is preservedPrevent data loss
Process MonitoringTrack script execution progressEnsure successful completion
Error LoggingCapture and review error detailsFacilitate troubleshooting

Automating the exclusion of sites from retention policies and the deletion of SharePoint Online sites provides significant advantages:

BenefitDescriptionImpact
Time EfficiencyReduces manual processing timeFrees IT resources
Error ReductionEliminates human error risksImproves accuracy
Policy ComplianceEnsures consistent applicationMaintains governance
ScalabilityHandles large volumes of sitesSupports growth
ScenarioApplicationValue
Project CleanupRemove completed project sitesMaintain organized environment
Policy UpdatesBatch policy exclusionsStreamline compliance
Resource ManagementFree up storage capacityOptimize infrastructure

Key Takeaway: This PowerShell script provides a powerful automation tool for administrators who need to manage SharePoint Online sites, retention policies, and Microsoft 365 Groups. By streamlining the process of excluding sites from retention policies and permanently deleting them, IT teams can ensure compliance with data retention policies while also efficiently managing site lifecycles.

By combining Microsoft Graph and PnP PowerShell cmdlets, this script provides a comprehensive solution for managing site retention and cleanup, helping organizations maintain a secure and compliant environment.