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 = ''

# 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


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
			Write-host $parentFolder
			Write-Host "Empty folder:" $Folder.Name "in" $ParentFolderURL -ForegroundColor Red


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

2 thoughts on “Remove Empty Folders in SharePoint Online”

  1. Hi, I am trying to use this script however I am receiving this error message:

    Delete-PnPEmptyFolder : Cannot process argument transformation on parameter ‘Folder’. Cannot convert the “Microsoft.SharePoint.Client.Folder” value of type “Microsoft.SharePoint.Client.Folder” to type “Microsoft.SharePoint.Client.Folder”.
    At line:72 char:23
    + Delete-PnPEmptyFolder $List.RootFolder
    + ~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Delete-PnPEmptyFolder], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Delete-PnPEmptyFolder

Leave a Comment