Remove Empty Folders in SharePoint Online - Step-by-Step Instructions
Harsh Sharma
SEO Specialist | Technical Analyst | Front-End Developer | Python & JavaScript Expert | Web Optimization & Performance Enhancement | Digital Marketing Enthusiast | Content Writing Strategist
Are you tired of seeing those empty folders in your SharePoint Online space? They can make finding files a hassle and take up space. But don’t worry, getting rid of them is easier than you might think! By cleaning out these empty folders, you’ll free up space and make it easier for everyone to find what they need. In this article, we’ll show you how to remove empty folders in SharePoint Online, so you can keep your digital workspace neat.
Reasons to Delete Empty Folders in SharePoint Online
Clearing empty folders in SharePoint Online is common right now, but do you ever think what are the reasons behind it? If yes, then the following points are explained to you the same simply:
Remove Empty Folders in SharePoint Online Via Power Automate
Trigger: Start with an appropriate trigger. For example, this flow should run on a schedule, manually, or based on some other trigger condition.
Get folders: Use the SharePoint REST API to list all folders in the document library you’re interested in. You can do this with a “Send an HTTP request to SharePoint” action. Make sure to specify the appropriate site URL and library path.
Check for empty folders: Iterate through the folders obtained in the previous step and check if they contain any files. You can do this using a “Apply to each” control and another HTTP action to get files within each folder.
Delete empty folders: Within the same loop, after checking if a folder is empty, use the SharePoint REST API to delete the empty folders.
Make sure to handle the condition where a folder is not empty carefully to avoid accidental deletions.
Handle errors: Implement error handling to manage cases where there might be permission issues or other errors preventing the deletion of folders.
Testing: Test the flow thoroughly before deploying it in a production environment, especially the deletion part, as it’s irreversible.
Also read: How Long Does Office 365 Keep Deleted Emails?
Using PowerShell Script to Remove Empty Folders in SharePoint Online
Before starting with this solution, it is crucial to know that initiating the operation with this solution will become a hassle for naive users. So, it is recommended to consider the aforementioned solution or further described smart solution.
# Add SharePoint Online CSOM Assemblies
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
# Remove Empty Folders Recurrently
function Remove-EmptyFolders($folder)
{
$ctx.Load($folder.Folders)
$ctx.ExecuteQuery()
foreach ($subFolder in $folder.Folders)
领英推荐
{
Remove-EmptyFolders $subFolder
}
# Delete the Folder, if it is empty
if ($folder.ItemCount -eq 0)
{
Write-Host “Deleting Folder: $($folder.ServerRelativeUrl)”
$folder.DeleteObject()
$ctx.ExecuteQuery()
}
}
# Set up credentials & SharePoint URL
$username = “[email protected]”
$password = “examplepassword”
# Connect to SharePoint Online
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, (ConvertTo-SecureString $password -AsPlainText -Force))
# Get the root folder of your SharePoint library
$rootFolder = $ctx.Web.GetFolderByServerRelativeUrl(“/sites/examplesite/LibraryName”)
# Call function to remove empty folders in SharePoint Online from the root folder
Remove-EmptyFolders $rootFolder
Write-Host “Empty folders removal completed.
So, with these PowerShell Codes, one can effortlessly initiate the operation in just a few minutes. But, what if you are not familiar with the technicalities? What would you do? Well, come down! Just opt for the SysTools SharePoint Migration Tool to troubleshoot the empty folder issue.
With this solution, one can migrate the extra or unused data to another account to free up space hassle-free. Plus, to operate this solution you don’t need to be familiar with technical how since it offers a user-friendly interface to understand its mechanism effortlessly. Furthermore, users can gain this tool for free by downloading it & getting the advantage of the advanced features without paying any cost.
Bring it All Together
Throughout this guide, we explored the solutions to troubleshoot the issue i.e. how to remove empty folders in SharePoint Online to clear files. Now, we know the answer to resolve the query manually & automatically. However, both described traditional methods are not up to the mark, but on the other hand, the specified automated solution has no limitations. Still, the choice is yours whether to opt for complex manual solutions or dependable smart solutions.