SharePoint Online: Restore Deleted Subsite from Recycle Bin using PowerShell

Requirement: The user had deleted a subsite by mistake. Now the requirement is to restore the deleted subsite in SharePoint Online.

How to restore a deleted subsite in SharePoint?

Have you ever accidentally deleted a subsite in SharePoint Online? If so, don’t worry – Deleted subsites in SharePoint Online are moved to the second-stage recycle bin and can be restored by the Site Collection Administrators. This blog post will walk you through the steps necessary to restore a deleted subsite in SharePoint Online. We will be using the SharePoint recycle bin and PowerShell methods to restore the subsite.

To restore a deleted subsite in SharePoint, follow these steps: 

  • Login to SharePoint Online Site collection as a site collection administrator
  • Navigate to Site Settings >> Click on “Recycle bin” under site collection administration
  • Click on “Second-stage recycle bin” link and select the desired subsite by clicking the checkbox of the subsite (Second stage recycle bin direct URL: https://<tenant>
  • Click on the “Restore” button in the ribbon to start restoring the subsite, and you’ll have your subsite back up and running in no time.
sharepoint online restore subsite from recycle bin

By default, deleted items are retained for 93 days in the recycle bin. If you don’t find your subsite under the recycle bin – Your site may be completely deleted! So you are left with restoring the content database backups in the SharePoint On-premises environment or raising a support ticket with Microsoft for SharePoint Online.

Restore Deleted Subsite from Recycle Bin using PowerShell

Here is how to restore deleted subsite in SharePoint Online using PowerShell:

#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Variables for Processing
$SiteUrl = ""
#Get the credentials to connect 
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
Try {    
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $Credentials
    #Get the Admin recycle bin
    $Site = $Context.Site
    $RecycleBinItems = $Site.RecycleBin
    #Frame DirName (Parent Site URL) and LeafName (Subsite URL)

    #Get the deleted subsite from Recycle bin
    $DeletedSubsite = $RecycleBinItems | Where { ($_.DirName -eq $DirName) -and ($_.LeafName -eq $LeafName)}
    If($DeletedSubSite -ne $Null)
        #Restore the first subsite found from Recylce bin
        Write-Host -f Green "Deleted Subsite restored Successfully!"
        Write-Host -f Yellow "Deleted Subsite Not Found in Recycle Bin!"
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red

SharePoint Online: PnP PowerShell to Restore Deleted SubSite

Let’s use PnP PowerShell to restore subsite SharePoint Online:

#Config Variables
$SiteURL = ""

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get Deleted Subsite
$DeletedSite = Get-PnPRecycleBinItem | Where { $_.Title -eq $SiteTitle -and $_.ItemType -eq "Web"}

#Restore Subsite
    Restore-PnPRecycleBinItem -Identity $DeletedSite -Force -ErrorAction Stop
    Write-Host -f Green "Subsite '$SiteTitle' restored successfully!"
    Write-host -f Yellow "Could not Find deleted Subsite:"$SiteTitle

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply