Remove Empty Folders in SharePoint Online

After merging duplicate folders in SharePoint Online I needed to remove the empty folders. This is something we can do as well with PowerShell and PnP.

# SharePoint url
$siteUrl = 'https://lazyadmin.sharepoint.com/'

# Site url
$site = 'sites/lab01'

# Library name
$libraryName = 'Duplicates'

# Set test mode
$whatIf = $true

# Set force mode
# Only set to true if you have fully tested it. Script WON'T ask for confirmation before deleting the file
$force = $false

#-----------------------------------------------------------[Functions]------------------------------------------------------------

#src https://www.sharepointdiary.com/2018/09/sharepoint-online-delete-empty-folders-using-powershell.html
Function Delete-PnPEmptyFolder([Microsoft.SharePoint.Client.Folder]$Folder)
{
    $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length)

    # 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)    

		if ($whatIf -ne $true)
		{
			#Delete the folder
			Write-Host "Remove folder:" $Folder.Name "in" $ParentFolderURL -ForegroundColor Red
			Remove-PnPFolder -Name $Folder.Name -Folder $ParentFolderURL -force:$force -Recycle
		}
		else
		{
			Write-host $parentFolder
			Write-Host "Empty folder:" $Folder.Name "in" $ParentFolderURL -ForegroundColor Red
		}
    }
}

#-----------------------------------------------------------[Execution]------------------------------------------------------------

# Login 
$url = $siteUrl + '/' + $site
Connect-PnPOnline -Url $url -UseWebLogin

# Cleanup empty folders
$Web = Get-PnPWeb
$List = Get-PnPList -Identity $libraryName -Includes RootFolder

Delete-PnPEmptyFolder $List.RootFolder

The script will go through all folders recursively. If the folder is empty then it will delete it. If you set the WhatIf variable to true at the top of the script, it will only show which folders it would delete. This way you can test the script before actually running it.

You can find the original version of the script here at Sharepointdairy.com. I only made a couple of small changes to the script. You can also get the full script here at my Github.

Get more stuff like this

IT, Office365, Smart Home, PowerShell and Blogging Tips

I hate spam to, so you can unsubscribe at any time.

Leave a Comment

0 Shares
Tweet
Pin
Share
Share