Deleting Empty Folders from the Document Library

Introduction

 
Mostly, empty folders look untidy in our document library. It's tiresome to discover and delete, so I am writing this script to find out the empty folders (Recursive) and delete them from the document library.
 
Download and Install the PNP modules:
 
for SPOnline- https://github.com/pnp/PnP-PowerShell/releases/download/3.20.2004.0/SharePointPnPPowerShellOnline.msi
for sp2019- https://github.com/pnp/PnP-PowerShell/releases/download/3.20.2004.0/SharePointPnPPowerShell2019.msi
for sp2016-https://github.com/pnp/PnP-PowerShell/releases/download/3.20.2004.0/SharePointPnPPowerShell2016.msi
for sp2013-https://github.com/pnp/PnP-PowerShell/releases/download/3.20.2004.0/SharePointPnPPowerShell2013.msi
 
Open Powershell online management shell or Windows powershell and run the following script (save files as .ps1 format).
  1. $SiteURL = "Your site url"  
  2. $DocLibName = "DocLibraryName"  
  3. #Connect to the Site  
  4. Connect-PnPOnline -URL $SiteURL -Credentials (Get-Credential)  
  5. #Get the web   
  6. $Web = Get-PnPWeb  
  7. #Get the List   
  8. $MYList = Get-PnPList -Identity $DocLibName -Includes RootFolder  
  9. Function Delete-ALLEmptyFolders([Microsoft.SharePoint.Client.Folder]$Folder)  
  10. {  
  11.     $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length)  
  12.     #Process all Sub-Folders  
  13.     $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder  
  14.     Foreach($SubFolder in $SubFolders)  
  15.     {  
  16.         #Exclude "Forms" and Hidden folders  
  17.         If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))))  
  18.         {  
  19.             #Call the function recursively  
  20.             Delete-ALLEmptyFolders -Folder $SubFolder  
  21.         }  
  22.     }  
  23.     #Get all files & Reload Sub-folders from the given Folder  
  24.     $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File  
  25.     $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder  
  26.    
  27.     If ($Files.Count -eq 0 -and $SubFolders.Count -eq 0)  
  28.     {  
  29.         #Delete the folder  
  30.         $ParentFolder = Get-PnPProperty -ClientObject $Folder -Property ParentFolder  
  31.         $ParentFolderURL = $ParentFolder.ServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length)     
  32.         Remove-PnPFolder -Name $Folder.Name -Folder $ParentFolderURL -Force -Recycle  
  33.         Write-Host -f Blue ("Deleted Folder: '{0}' at '{1}'" -f $Folder.Name, $Folder.ServerRelativeURL)  
  34.     }  
  35.    
  36. }  
  37. #Call the Function to Delete empty Folders  
  38. Delete-ALLEmptyFolders $MYList.RootFolder  
 That's it. After successful execution, all the empty folders in the document library get deleted. I hope this helps someone. Happy coding! :)