Home » Blog » SharePoint Online » Delete Empty Folders in SharePoint Online Using Admin Center, PowerShell, and Power Automate

Delete Empty Folders in SharePoint Online Using Admin Center, PowerShell, and Power Automate

author
Published By Raj Kumar
Anuraag Singh
Approved By Anuraag Singh
Published On October 8th, 2024
Reading Time 7 Minutes Reading

Undoubtedly, SharePoint Online is a powerful tool for ensuring collaboration and document management. However, sometimes due to its excessive usage the created folders have never been used and cluttered the document libraries. As a result, it becomes challenging for the users to get the essential data efficiently. So, deleting these empty folders is crucial for maintaining the SharePoint environment as clean and efficient. In this article, we will walk through the three methods: SharePoint Admin Center, PowerShell, and Power Automate to delete empty folders in SharePoint Online. But first, find the benefits of removing the empty folders in SharePoint.

Top 4 Advantages of Deleting the Empty Folders in SharePoint Online

  • Improved Navigation – Removing the empty folders from SharePoint Online declutters the document library. Due to this, the users can easily navigate to the files and folders they want.
  • Enhanced Search Capabilities – The search filter of SharePoint is also improved and it will start taking less time to find the right data after deleting the empty folders.
  • Optimized Storage – Removing the unwanted and empty folders minimized the storage space.
  • Accurate Permissions – After cleaning up the SharePoint document library from the empty folders. It also becomes easier for the administrators to assign the permissions to the correct folders only.

PowerShell Commands to Delete Empty Folders in SharePoint Online

You can also determine the count of empty folders using the folder child and item child count options. To do so, open the Document Library> Edit current view. Now choose the Folder child and Item child count options then press OK. By executing the below PowerShell commands in the PowerShell, you can remove the empty folders. But before executing them, also make sure:-

  • You are well-versed in Shell commands.
  • You have the Global Administrator permissions.
$Urlofsite = '<Your–site-URL>'

$site = 'sites/lab01'

$Nameoflibrary = 'Duplicates'

$whatIf = $true

$force = $false

Function Delete-PnPEmptyFolders([Microsoft.SharePoint.Client.Folder]$Folder) {
$FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length)

$allSubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder
foreach ($SubFolder in $allSubFolders) {

if (($SubFolder.Name -ne "Forms") -and (-not ($SubFolder.Name.StartsWith("_")))) {

Delete-PnPEmptyFolders -Folder $SubFolder
}
}

$AllFiles = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File
$allSubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder
if ($AllFiles.Count -eq 0 -and $allSubFolders.Count -eq 0) {
$GetParentFolder = Get-PnPProperty -ClientObject $Folder -Property GetParentFolder
$GetParentFolderURL = $GetParentFolder.ServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length)
if ($whatIf -ne $true) {
Write-Host "Removing folder:" $Folder.Name "in" $GetParentFolderURL -ForegroundColor Red
Remove-PnPFolder -Name $Folder.Name -Folder $GetParentFolderURL -Force:$force -Recycle
} else {
Write-Host "Empty folder:" $Folder.Name "in" $GetParentFolderURL -ForegroundColor Red
}
}
}

Function Connect-To-the-SharePointOnline($url) {
Connect-PnPOnline -Url $url -UseWebLogin
if (-not $?){
Write-Host "connection to SharePoint Online is failed" -ForegroundColor Red
exit
}
}

$url = $Urlofsite + '/' + $site

Connect-To-the-SharePointOnline -url $url

$Web = Get-PnPWeb
$List = Get-PnPList -Identity $Nameoflibrary -Includes RootFolder
Delete-PnPEmptyFolders -Folder $List.RootFolder

Delete All Empty Folders in SharePoint Online Using Power Automate

Go with the below steps to clear empty folders using Power Automate.

Step 1. Launch the Power Automate and hit on the Instant Flow option.
Step 2. Provide a name and select manually trigger a flow option.
Step 3. Click on Create and then in Edit mode hit on the + button.
Step 4. Choose the Send an HTTP request to SharePoint.
Add the Site from where you want to initiate the flow, and set the methods property as GET.
Step 5. Uri needs to resemble the following “_api/web/GetFolderByServerRelativeUrl(‘/sites/SiteName/NameOfDocumentLibrary’)/”.
Step 6. Enter the below values in the header properties
Key – accept
Value – application/json;odata=nometadata

Step-7. Create an array for filtering the result. To do so, use the below expression body(‘GetFolders’)?[‘value’]. Press the “Edit in advanced mode” and then enter the expression: @and(equals(item()?[‘ItemCount’], 0), not(equals(item()[‘Name’], ‘Forms’))).
Step-8. Now, add then another action of the same category. Select “Send an HTTP request to SharePoint” and finalize the “Apply to each” in the action column.
Step-9. Put the Address of the site in the method and pick DELETE. Use the exact Uri property as discussed above.
Step 10. Finally, at last of the Uri includes:
“(items(‘ForEach_Folder’)[‘Name’])”. Then Save and deploy the Flow.

Best Practices to Manage the SharePoint Online Folders

  1. You can regularly monitor the SharePoint Online folders and identify the empty ones.
  2. Educate users regarding the importance of the SharePoint document library. Also, advise them to avoid the creation of unnecessary folders.
  3. Keep them updated about the metadata, so that the document retrieval process can be efficient.
  4. Spread awareness between the team members about the benefits of PowerShell commands and Power Automate to simplify the deletion of empty folders.

How to Delete Empty Folders in SharePoint Online Using Admin Center?

The SharePoint administrator offers an interactive way to interact with SharePoint Online. While it does not provide any direct approach to remove the empty folders. So, you need to identify and delete them manually.

  • Step 1. Navigate to the Microsoft 365 Admin Center and then open the SharePoint Admin Center.
  • Step 2. Move to the site from where you want to delete the empty folders.
  • Step 3. Find the empty folders manually.
  • Step 4. Press right-click on the empty folder and choose the Delete option to delete that folder.

After the deletion of the SharePoint empty folders, these will go into the recycle bin. And stay there as per the retention period. But what if you mistakenly delete the SharePoint non-empty folder? To keep their data safe and secure from these unexpected threats, most organizations use another SharePoint site. It gives the data an additional degree of security.

How to Add an Extra Layer of Security to SharePoint?

You can use the Most Impeccable SharePoint Migration Tool. It is composed of multiple advanced features. It can easily perform SharePoint tenant to tenant migration without any data loss. You do not need to worry about the execution of this tool. It requires some easy and quick steps to accomplish the process.

Download Now Purchase Now

Conclusion

Managing the SharePoint Online environment organized and uncluttered. You can use the above-mentioned PowerShell commands, Admin Center, and Power Automate steps. Through all of these methods, you can easily delete empty folders in SharePoint Online. Additionally, we have also suggested a professional tool to safeguard your data from unexpected or mistaken data loss.

Frequently Asked Questions

Q1. Can Microsoft’s Power Automate tool remove empty folders in SharePoint Online?

A –  Yes, you can delete all the empty folders using Power Automate. Let’s see how.

  1. Run the Power Automate and Open Instant Flow.
  2. Assign a name and choose manually trigger.
  3. Create and press (+) from Edit mode.
  4. Select Send an HTTP request.
  5. Complete the required credentials and add the Site Address then Delete.

Q2. How to clear all empty folders from SharePoint Online?

A – Follow the below steps:-

  1. Open SharePoint Admin Center.
  2. Launch the site.
  3. Search for the empty folders.
  4. Press right-click and delete them individually.

Q3. What are the limitations of using PowerShell commands to delete the empty folders?

A – There are several limitations of using PowerShell such as:-

  1. You need to be good at PowerShell commands.
  2. You cannot rely completely on getting accurate results.
  3. Huge chances of data loss.

Connect With Us

+9111-28084986