PnP PowerShell: Permanently delete site collections from Recycle Bin

One of the important action that I really miss in SharePoint Online Admin Center is the ability to delete sites from the recycle bin. If we go to SharePoint Admin Center and navigate to Deleted Sites (Modern UI) or Recycle Bin (Classic UI), we get a list of sites which are due to be deleted. When any of the site is selected from that list, it only shows “Restore” option, no “Delete” option.

Classic UI

ClasssicUI

Modern UI

MOdernUI

Using PnP PowerShell commands, we can get the list of site collections in tenant recycle bin. Then by iterating each of the site collection, delete operation can be performed.

For working with PnP PowerShell, you can also refer this article.

Connect to Tenant

First of all we need to connect to SharePoint tenant using below command and providing the credentials on prompt.

Connect-PnPOnline -Url "<https://tenant-name.sharepoint.com>"

Get Recycle Bin Items

Once connection is setup successfully, we need to execute below command to get list of all deleted site collections. We will collect the output in a variable so that we can iterate through the list at later stage.

$items = Get-PnPTenantRecycleBinItem

You can also view the list of items by simply running the command “Get-PnPTenantRecycleBinItem“. This will display the list of all site Urls which are already deleted or yet to be deleted in tabular format along with information like DaysRemaining and DeletionTime.

DeletedSites

Delete Recycle Bin Item

Lets iterate through the list of items and check if DaysRemaining is greater than zero i.e. site is yet to be deleted, then permanently delete that site collection. If we do not apply this condition, the we will get error while deleting the site as it may already been deleted.

Error

foreach($item in $items)
{
    if($item.DaysRemaining -gt 0)
    {
       Write-Host "Site to be deleted permanently: " $item.Url -ForegroundColor Green
       Clear-PnPTenantRecycleBinItem -Url $item.Url -Wait -Force
    }
}

Clear-PnPTenantRecycleBinItem” command is used to permanently delete site collection from recycle bin.

Wait” attribute is used to wait for the operation to complete before executing further powershell commands.

Force” attribute is used to bypass the confirmation prompt asked for deleting the site.

Confirm

Usually this is helpful for us developers who create and delete lot of site collections in tenant. While creating new site, we may give name which was already being used by the deleted site.

Hope this is useful. Please do share your feedback in comments section.

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s