Automating SharePoint Site Deletion and Retention Policy Updates with PowerShell

5 min. readlast update: 10.13.2024

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.

In this article, we will break down the script, which performs two key functions:

  1. Excluding SharePoint sites from a retention policy.
  2. Permanently deleting SharePoint Online sites and their corresponding Microsoft 365 Groups.

PowerShell Script Overview

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.

Prerequisites:

Before running the script, ensure you have the following:

  • Administrative permissions in Microsoft 365 Compliance Center and SharePoint Online.
  • Installed PnP PowerShell and appropriate modules for accessing retention policies and SharePoint sites.
  • Registered a compliance policy that governs the retention of SharePoint content.

1. Excluding SharePoint Sites from a Retention Policy

The script starts by excluding a list of SharePoint Online sites from a specific retention policy. Retention policies are often used to ensure that content is retained for a specific period to comply with legal and business regulations.

How It Works:

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

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

     
    $csv = Import-Csv -Path C:\Path\To\Your\CSVFile.csv $SitesURL = $csv | Select-Object -ExpandProperty url
  3. Excluding Sites from Retention Policy: The script retrieves the existing retention policy by name and then adds the SharePoint site URLs to the exclusion list.

     
    $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.

  4. Error Handling: 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.

2. Deleting SharePoint Sites and Associated Microsoft 365 Groups

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.

How It Works:

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

     
    Connect-PnPOnline -Url $AdminCenterURL -Interactive
  2. Iterating Through Each Site: The script loops through the list of SharePoint sites and performs the following actions:

    • Retrieve the SharePoint Site: It retrieves the site information using Get-PnPTenantSite.

    • Delete the SharePoint Site: The Remove-PnPTenantSite cmdlet permanently deletes the SharePoint site. The -Force parameter skips the recycle bin, ensuring the site is fully deleted.

       
      Remove-PnPTenantSite -Url $SiteUrl -Force
    • Delete the Associated Microsoft 365 Group: The script then deletes the Microsoft 365 Group that is associated with the SharePoint site using Remove-PnPMicrosoft365Group.

       
      Remove-PnPMicrosoft365Group -Identity $Site.GroupId
  3. Handling Errors During Deletion: The script also includes a try/catch block to log any errors encountered during the site or group deletion process, allowing administrators to troubleshoot issues quickly.

Best Practices for Running the Script:

  • Test Before Running in Production: Before running the script in a live environment, test it in a development or test tenant to ensure it works as expected.

  • Backup Data: Ensure that all critical data is backed up before deleting SharePoint sites and Microsoft 365 Groups, especially if retention policies are being altered.

  • Monitor the Process: While the script automates the task, it's important to monitor its execution to ensure that all sites and groups are properly excluded and deleted.

Why Automate These Tasks?

Automating the exclusion of sites from retention policies and the deletion of SharePoint Online sites saves time and reduces the risk of human error. This is particularly useful when dealing with a large number of sites that need to be removed, ensuring that policies are correctly enforced and sites are cleaned up efficiently.

Conclusion

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.

Was this article helpful?