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 or the end-user ever accidentally deleted a subsite in SharePoint Online? Did anyone with full control permissions hit the delete button by mistake? (In modern team sites and communication sites, it’s available for all end users with full control permissions). 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: 

  1. Login to SharePoint Online Site collection as a site collection administrator
  2. Click on the settings gear icon >> Site Information >> View all Site Settings
  3. Click on “Recycle bin” under site collection administration to get into the site collection recycle bin.
  4. Click on the “Second-stage recycle bin” link and select the desired subsite by clicking the checkbox of the subsite (Second stage recycle bin URL: https://<tenant>.sharepoint.com/sites/Marketing/_layouts/15/AdminRecycleBin.aspx?view=13)
  5. Click on the “Restore” button in the command bar 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 SharePoint sites are retained for 93 days in the recycle bin. If you don’t find your sub sites 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. Instructions to restore the site may differ a bit on modern sites/group sites.

Restore Deleted Subsite from Recycle Bin using PowerShell

How do I recover a deleted subsite with 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 = "https://crescent.sharepoint.com/sites/Marketing"
$DeletedSubsiteURL="/sites/marketing/2018/docs"
 
#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
    $Context.Load($Site)
    $Context.Load($RecycleBinItems)
    $Context.ExecuteQuery()
 
    #Frame DirName (Parent Site URL) and LeafName (Subsite URL)
    $DirName=$DeletedSubsiteURL.Substring(1,$DeletedSubsiteURL.LastIndexOf("/")-1)
    $LeafName=$DeletedSubsiteURL.Substring($DeletedSubsiteURL.LastIndexOf("/")+1)

    #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
        $DeletedSubSite[0].Restore()
        $Context.ExecuteQuery()
        Write-Host -f Green "Deleted Subsite restored Successfully!"
    }
    else
    {
        Write-Host -f Yellow "Deleted Subsite Not Found in Recycle Bin!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Once the subsite has been restored to its destination, you’ll be able to access it and any associated site contents with its original files just as you did before.

SharePoint Online: PnP PowerShell to Restore Deleted SubSite

Let’s use the PnP PowerShell command Restore-PnPRecycleBinItem to restore subsite SharePoint Online. This PowerShell script first connects to SharePoint Online site. Then, gets all deleted subsites with “Get-PnPRecycleBinItem” cmdlet, including the subsite you want to restore. Finally, restores the subsite, using the “Restore-PnPRecycleBinItem” PowerShell cmdlet. Just specify the URL of the subsite and run the script. The subsite will be restored to its original location.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$SiteTitle="Branding"

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

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

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

To restore a SharePoint site collection in SharePoint Online from the SharePoint admin center or PowerShell, use: SharePoint Online: How to Restore a Deleted Site Collection using PowerShell?

How do I restore a document in SharePoint Online?

To restore a document in SharePoint Online, follow these steps: Log into your SharePoint Online site, Click on the Settings icon >> Choose the “Site Contents” and Click the Recycle Bin link. Select each item you want to restore on the Recycle Bin page and click the “Restore” button.
More info: SharePoint Online: Restore Deleted Items from Recycle Bin using PowerShell

How to restore deleted a folder on SharePoint?

To restore a deleted folder in the SharePoint Online document library, navigate to your SharePoint site >> Click on the Settings gear, and choose the “Site Contents” link. On the site contents page, click on Recycle bin link. Select the folder to restore from the recycle bin and click on the “Restore” button from the toolbar.
More info: Restore a Deleted Folder in SharePoint Online with PowerShell

How do I delete a subsite in SharePoint Online using PowerShell?

Use the “$web.DeleteObject()” CSOM method to delete a subsite in Sharepoint Online. With PnP PowerShell, you can use the “Remove-PnPWeb” cmdlet.
More info: PowerShell to Delete subsite in SharePoint Online

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *