SharePoint Online - Clear All Empty Folders

J.P. Lesser 0 Reputation points
2024-02-20T16:57:56.5733333+00:00

I have a client whose SharePoint Online is a little out of control with respect to Empty folders. I was looking for any suggestions on methods to search for, then delete all empty folders? I assume there is Powershell scripting which can take care of this? Any help or suggestions would be greatly appreciated

Microsoft 365 and Office | SharePoint | Development
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Xyza Xue_MSFT 30,176 Reputation points Microsoft External Staff
    2024-02-21T02:17:36.7966667+00:00

    Hi @J.P. Lesser ,

    We can find empty folders in SharePoint Online from the web user interface by using Item Child Count and Folder Child Count .

    1. Navigate to the document library >> Click on “Edit Current View” from the Views drop-down.
    2. Select “Folder Child Count” and “Item Child Count” columns and Click on OK.

    sharepoint online delete empty folders

    PnP PowerShell to Delete Empty Folders in SharePoint Online:

    #Parameters
    $SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
    $DocumentLibraryName = "Documents"
      
    #Connect to the Site
    Connect-PnPOnline -URL $SiteURL -Credentials (Get-Credential)
     
    #Get the web & folder
    $Web = Get-PnPWeb
    $List = Get-PnPList -Identity $DocumentLibraryName -Includes RootFolder
     
    Function Delete-PnPEmptyFolder([Microsoft.SharePoint.Client.Folder]$Folder)
    {
        $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length+1)
        #Process all Sub-Folders
        $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder
        Foreach($SubFolder in $SubFolders)
        {
            #Exclude "Forms" and Hidden folders
            If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))))
            {
                #Call the function recursively
                Delete-PnPEmptyFolder -Folder $SubFolder
            }
        }
        #Get all files & Reload Sub-folders from the given Folder
        $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File
        $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder
     
        If ($Files.Count -eq 0 -and $SubFolders.Count -eq 0)
        {
            #Delete the folder
            $ParentFolder = Get-PnPProperty -ClientObject $Folder -Property ParentFolder
            $ParentFolderURL = $ParentFolder.ServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length+1)   
            Remove-PnPFolder -Name $Folder.Name -Folder $ParentFolderURL -Force -Recycle
            Write-Host -f Green ("Deleted Folder: '{0}' at '{1}'" -f $Folder.Name, $Folder.ServerRelativeURL)
        }
     
    }
    #Call the Function to Delete empty Folders
    Delete-PnPEmptyFolder $List.RootFolder
    

    Remember to replace the variable $SiteURL, *$*DocumentLibraryName with your own.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. J.P. Lesser 0 Reputation points
    2024-02-28T22:16:53.2533333+00:00

    Same issue Connect-PnPOnline : The term 'Connect-PnPOnline' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:9 char:1

    • Connect-PnPOnline -URL $SiteURL -Credentials (Get-Credential)
    •     + CategoryInfo          : ObjectNotFound: (Connect-PnPOnline:String) [], CommandNotFoundExcep 
         tion
          + FullyQualifiedErrorId : CommandNotFoundException
       
      

    Get-PnPWeb : The term 'Get-PnPWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:12 char:8

    • $Web = Get-PnPWeb
    •    ~~~~~~~~~~
      
      • CategoryInfo : ObjectNotFound: (Get-PnPWeb:String) [], CommandNotFoundException
      • FullyQualifiedErrorId : CommandNotFoundException

    Get-PnPList : The term 'Get-PnPList' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:13 char:9

    • $List = Get-PnPList -Identity $DocumentLibraryName -Includes RootFold ...
    •     ~~~~~~~~~~~
      
      • CategoryInfo : ObjectNotFound: (Get-PnPList:String) [], CommandNotFoundException
      • FullyQualifiedErrorId : CommandNotFoundException

    Unable to find type [Microsoft.SharePoint.Client.Folder]. At line:15 char:32

    • ... on Delete-PnPEmptyFolder([Microsoft.SharePoint.Client.Folder]$Folder)
    •                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
      • CategoryInfo : InvalidOperation: (Microsoft.SharePoint.Client.Folder:TypeName) [], RuntimeException
      • FullyQualifiedErrorId : TypeNotFound

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.