Precise Instructions to Remove Empty Folders in SharePoint Online

Harsh Sharma
4 min readJun 24, 2024

--

Remove Empty Folders in SharePoint Online

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:

  1. Removing empty folders frees up storage, ensuring efficient and cost-effective SharePoint Online usage.
  2. Clearing clutter improves user experience, aiding navigation and boosting productivity.
  3. Removing empties prevents confusion, ensuring files are easily found in logical locations.
  4. Cleaning removes clutter, refining search accuracy, and facilitating quick file retrieval.
  5. Less clutter streamlines management tasks, like permissions and content updates, promoting efficiency.

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.

  • Method: GET
  • Uri: _api/web/getfolderbyserverrelativeurl(‘<your library path>’)/folders

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.

  • Inside the loop, use another “Send an HTTP request to SharePoint” action to get the files within each folder:
  • Method: GET
  • Uri: _api/web/getfolderbyserverrelativeurl(‘<folder URL>’)/files

Delete empty folders: Within the same loop, after checking if a folder is empty, use the SharePoint REST API to delete the empty folders.

  • Method: DELETE
  • Uri: _api/web/getfolderbyserverrelativeurl(‘<folder URL>’)

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

$siteUrl = “https://examppletenant.sharepoint.com/sites/yourexamplesite"

$username = “exampleusername@exampletenant.onmicrosoft.com”

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

--

--

Harsh Sharma

An experienced technical analyst specializing in Microsoft Office 365. Expertise in seamless transitions, optimizing workflows, and enhancing collaboration.