Removes all sites from the SharePoint Online recycle bin.

The Remove-CT365AllSitesFromRecycleBin function connects to SharePoint Online using the provided admin URL and then removes all sites from the SharePoint Online recycle bin. This function requires the PSFramework and PnP.PowerShell modules.

Specifies the URL of the SharePoint Online admin center. The URL must follow the format ''.

Remove-CT365AllSitesFromRecycleBin -AdminUrl ""

Connects to the SharePoint Online admin center at '' and removes all sites from the recycle bin.



Please add any suggestions or issues to GitHub Issues


function Remove-CT365AllSitesFromRecycleBin {
    Param (
                if ($_ -match '^[a-zA-Z0-9]+\.sharepoint\.[a-zA-Z0-9]+$') {
                else {
                    throw "The URL $_ does not match the required format."

    Begin {
        # Check if required modules are available, otherwise install them
        foreach ($module in @('PSFramework', 'PnP.PowerShell')) {
            if (-not (Get-Module -ListAvailable -Name $module)) {
                Install-Module $module -Scope CurrentUser

        # Connect to SharePoint Online
        Connect-PnPOnline -Url $AdminUrl -Interactive

    Process {
        try {
            # Retrieve sites from recycle bin
            $recycleBinItems = Get-PnPTenantRecycleBinItem

            # Delete sites from recycle bin
            foreach ($item in $recycleBinItems) {
                Remove-PnPTenantDeletedSite -Identity $item.Url -Force
                Write-PSFMessage -Message "Site removed from recycle bin: $($item.Url)" -Level Host

            if ($recycleBinItems.Count -eq 0) {
                Write-PSFMessage -Message "No sites found in the recycle bin." -Level Host
            else {
                Write-PSFMessage -Message "All sites have been removed from the recycle bin." -Level Host
        catch {
            Write-PSFMessage -Message "An error occurred: $_" -Level Error

    End {
        Disconnect-PnPOnline -ErrorAction SilentlyContinue