A SharePoint library is a location on a site where you can upload, create, update, and collaborate on files with team members. To remove a empty folder from the library it is do difficult, since we need to go to each and every library and identify whether it is empty or not. Then we have to remove it from the library. To automate this process, I used Powershell scripting because it is easy to parse through each and every library using PNP library. So it is easy to identify and remove the empty folders in SharePoint library.
Prerequisite
Before you begin utilizing PowerShell to oversee SharePoint Online, ensure that the SharePoint Online Management Shell is installed. You can install the SharePoint Online Management Shell by downloading and running the SharePoint Online Management Shell. You only need to do this once for each computer from which you are running SharePoint Online PowerShell commands.
1. Connect to Site
Connect to SharePoint site using Connect-PnPOnline cmdlet. The required parameters are,
Url – The SharePoint site url (Eg: https://hubflysoft.sharepoint.com/sites/Hubfly)
The following code snippet helps to connect SharePoint sites.
$siteurl=”https://<tenant-name>.sharepoint.com”
Connect-PnPOnline -Url $siteurl
2. Get Web Instance using Get-PnPWeb
$web = Get-PnPWeb
The Get-PnPWeb cmdlet actually has its own Recurse option, and you could use that if you want to rather than doing your own recursion.
3. Get User Context using Get-PnPContext
context = Get-PnPContext
The Get-PnPContext returns a Client Side Object Model context. This command is very handy and it returns the current context. Why u need context it might be the case you need to save the current context. By switch between two sites you need this
4. Get folders from the library using Get-PnPFolder
$folder = Get-PnPFolder -RelativeUrl “Shared Documents”
The Get-PnPFolder returns the folder called ‘Shared Documents’ which is located in the root of the current web.
5. Get all sub folders and remove empty folders
Using the context we need to load all files,folders and Parent folder like below code snippet
$files = $folder.Files
$context.Load($folder.Files)
$context.Load($folder.Folders)
$context.Load($folder.ParentFolder)
$context.ExecuteQuery()
We can find the folder is empty by following conditions,
1. Check the files count is equal to zero
2. Check the folders count is equal to zero
And remove the empty folders using below PS script
Remove-PnPFolder
if ($folder.Files.Count -eq 0 -and $folder.Folders.Count -eq 0 -and (($folder.Name -notmatch ‘Document’) -and ($folder.Name -notmatch $libraryName )))
{
$path = $folder.ParentFolder.ServerRelativeUrl.Substring($web.ServerRelativeUrl.Length)
Write-Host “Removing folder ” $folder.ServerRelativeUrl.Substring($web.ServerRelativeUrl.Length)
Remove-PnPFolder -Folder $path -Name $folder.Name -Recycle -Force
$folders_list += $folder.Name + “, ” + $folder.ServerRelativeUrl
}
Write-Host “Searching for empty folders. Please wait…”
$folders_list = GetAllSubFolders $folder $context
Write-Host $libraryName ‘Completed’
Final code:
$siteUrl = “https://<tenant-name>.sharepoint.com/sites/<site-name>”
$libraryUrl = “Shared%20Documents”
$libraryName = “Documents”
Connect-PnPOnline -Url $siteUrl
$web = Get-PnPWeb
$context = Get-PnPContext
$folder = Get-PnPFolder -RelativeUrl $libraryUrl
$folders_list=@()
Function GetAllSubFolders([Microsoft.SharePoint.Client.Folder]$folder, [Microsoft.SharePoint.Client.ClientContext] $context)
{
$files = $folder.Files
$context.Load($folder.Files)
$context.Load($folder.Folders)
$context.Load($folder.ParentFolder)
$context.ExecuteQuery()
foreach($subFolder in $folder.Folders)
{
GetAllSubFolders $subFolder $context
}
if ($folder.Files.Count -eq 0 -and $folder.Folders.Count -eq 0 -and (($folder.Name -notmatch ‘Document’) -and ($folder.Name -notmatch $libraryName )))
{
$path = $folder.ParentFolder.ServerRelativeUrl.Substring($web.ServerRelativeUrl.Length)
Write-Host “Removing folder ” $folder.ServerRelativeUrl.Substring($web.ServerRelativeUrl.Length)
Remove-PnPFolder -Folder $path -Name $folder.Name -Recycle -Force
$folders_list += $folder.Name + “, ” + $folder.ServerRelativeUrl
}
return $folders_list
}
cls
Write-Host “Searching for empty folders. Please wait…”
$folders_list = GetAllSubFolders $folder $context
Write-Host $libraryName ‘Completed’
Hope you learnt how to remove empty folder and files in the SharePoint library programmatically using PnP PowerShell scripting. The operations mentioned above are tested on SharePoint Online environment. Feel free to fill up the comment box below, if you need any assistance.