List, Remove And Restore Deleted Site Using Microsoft 365 CLI

Introduction

 
Your deleted current SharePoint sites will not vanish from the UI before they have been eliminated from the recycle bin in the tenant. Erased SharePoint destinations are helping for 93 days. Sites, all their substance and settings are for all time erased, including records, libraries, pages, and any subsites.

To reuse a URL from an erased site(reproduce a site), you need to erase the site, it may require as long as 24 hours for the URL to open up. You can either hang tight for a quarter of a year. Erase them physically by the SharePoint administrator, or run the CLI for Microsoft 365 content underneath.
 

Installation of Microsoft 365 CLI

 
Regardless of the off chance that you are on Windows, macOS, or Linux, utilizing bash, Cmder or PowerShell, utilizing the CLI for Microsoft 365 you can design Microsoft 365, oversee SharePoint Framework ventures, and construct computerization.
 
The CLI for Microsoft 365 is distributed as an NPM package. To use it, install it using the below command, 
  1. npm i -g @pnp/cli-microsoft365  
  1. m365 login  
List Remove and Restore Deleted Site using Microsoft 365 CLI

Copy https://microsoft.com/devicelogin and paste it into the browser,
 
List Remove and Restore Deleted Site using Microsoft 365 CLI 
 
Get all Deleted Sites from Tenant Recycle Bin,
  1. m365 spo tenant recyclebinitem list [options]  
Yield as underneath,
  1. C:\Users\Dips3>m365 spo tenant recyclebinitem list    
  2.   
  3. DaysRemaining  DeletionTime                                             Url    
  4. -------------  -------------------------------------------------------  -------------------------------------------------------    
  5. 92             Fri May 07 2021 15:30:55 GMT+0530 (India Standard Time)  https://dev1802.sharepoint.com/sites/SampleTeamSite    
  6. 92             Fri May 07 2021 15:30:52 GMT+0530 (India Standard Time)  https://dev1802.sharepoint.com/sites/Viva-Communication    
  7. 92             Fri May 07 2021 15:30:56 GMT+0530 (India Standard Time)  https://dev1802.sharepoint.com/sites/Viva-Team    
  8. C:\Users\Dips3>m365 spo tenant recyclebinitem list -o json    
  9. [    
  10.   {    
  11.     "_ObjectType_""Microsoft.Online.SharePoint.TenantAdministration.DeletedSiteProperties",    
  12.     "_ObjectIdentity_""6c9bc59f-b03f-0000-7fdb-2a757bad1669|908bed80-a04a-4433-b4a0-883d9847d110:3d24e369-128a-419f-a457-659738339df3\nDeletedSiteProperties\nhttps%3a%2f%2fdev1802.sharepoint.com%2fsites%2fSampleTeamSite",    
  13.     "DaysRemaining": 92,    
  14.     "DeletionTime""/Date(2021,4,7,15,30,55,3)/",    
  15.     "SiteId""/Guid(71145661-b598-4780-925c-25df3a7b2353)/",    
  16.     "Status""Recycled",    
  17.     "StorageMaximumLevel": 26214400,    
  18.     "Url""https://dev1802.sharepoint.com/sites/SampleTeamSite",    
  19.     "UserCodeMaximumLevel": 300    
  20.  }  
  21. ]  
Remove site from recycle bin permanently,
  1. m365 spo tenant recyclebinitem remove [options]  
Yield as underneath,
  1. C:\Users\Dips3>m365 spo tenant recyclebinitem remove --url  https://dev1802.sharepoint.com/sites/Viva-Team  
  2. ? Are you sure you want to remove the deleted site collection https://dev1802.sharepoint.com/sites/Viva-Team from tenant recycle bin? Yes    
Restore Deleted Site,
  1. m365 spo tenant recyclebinitem restore [options]  
Yield as underneath,
  1. C:\Users\Dips3>m365 spo tenant recyclebinitem restore --url https://dev1802.sharepoint.com/sites/Viva-Communication  
Delete all sites permanently in one shot,
  1. $Sites = m365 spo tenant recyclebinitem list -o json | ConvertFrom-Json    
  2. $Sites | Format-Table Url    
  3. if($Sites.Count -gt 0){    
  4.  for($index=0; $index -lt  $Sites.Count; $index++)    
  5.  {     
  6.    m365 spo tenant recyclebinitem remove -u $Sites[$index].Url --confirm    
  7.  }    
  8. }     
Reference URLs
happy coding