Deleting Folders - Nero Blanco IT Migration Specialists
-
Twan van Beers
-
03 June, 2015
from an Exchange Mailbox with PowerShell
I was given an escalation at a client where somehow a user managed to create thousands of folders all nested one inside the other. So the task was how to delete these folders.
User and 1/2/3rd line support had already tried using Outlook, OWA and MFCMapi but none of them worked for the user anymore (plus personally the thought of using MFCMapi makes me shudder…)
First thought, surely you can just delete them with PowerShell…? Alas, there is no Remove-MailboxFolder, nor does eDiscovery help with the search and destroy feature as this is only for mail, not folders.
Right so what is left…? EWS to the rescue, however the thought of doing EWS with PowerShell did make me think twice. I did a quick search on Google and found Delete Outlook Folders Bottom-Up. To make it run you need the EWS Managed API, which can be downloaded from the Microsoft site. The current version at time of writing is v2.2 found here
I took a quick look through the code though and found that actually it isn’t paging through the results properly and so won’t work for more than 1000 folders. So I set about fixing it.
First was to add paging which was surprisingly simple. The following code snippet shows how the paging works by adjusting the view’s offset
$MoreResults = $true do{ $Response = $RootFolder.FindFolders($FolderView) $AllSubFolders += $Response.Folders $MoreResults = $Response.MoreAvailable $FolderView.Offset += $Response.Folders.count } while( $MoreResults -eq $true )Then deleting the folders from the bottom up. Since I have all of the folders in an array and the folders are pulled out in the order they were created (therefore the parent folder always exists before the lower level one), we can simply reverse our way through the array and delete them.
for( $i = ($AllSubFolders.Count - 1); $i -gt 0; $i-- ) { $AllSubFolders[$i].Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete) }Even after doing those two pieces I struggled because the sheer number of folders meant that EWS was timing out. Not to fear since we can catch that!
for( $i = ($AllSubFolders.Count - 1); $i -gt 0; $i-- ) { $error.clear() $AllSubFolders[$i].Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete) if( $error[0] -like '*the operation has timed out*' ) { $i++ } }Finally I didn’t like how the original code tried to find the top level folder so changed it from looping through all folders to just searching for the one with the right display name
$SearchFilter = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo( [Microsoft.Exchange.WebServices.Data.FolderSchema]::Displayname, $topFolderName ) $Response = $RootFolder.FindFolders($SearchFilter, $FolderView)Putting it all together and we get a pretty cool (albeit dangerous) script. Since the original script was on MS Technet Gallery I uploaded this revision there too. Delete Outlook Folders Bottom-Up (v2)
The bottom line for me was that actually using EWS from Powershell isn’t actually that difficult, and adds some very powerful tools to the Exchange Admin’s toolbelt
Enjoy!
Twan van Beers
Twan is a senior consultant with over 30 years of experience. He has a wide range of skills including Messaging, Active Directory, SQL, Networking and Firewalls. Twan loves to write scripts and get deep and dirty into debugging code, in order to understand and resolve the most complex of problems.
News & Insights
Trusted insights on technology and innovation to power your business growth.
-
03/06/2026
A practical engineering guide to owners, assignment, consent, credentials and ...
Read More
-
19/05/2026
Domain migrations in Microsoft 365 are rarely as simple as shifting email ...
Read More
-
12/05/2026
Over the past 10 to 15 years, Microsoft 365 has fundamentally reshaped how ...
Read More
-
07/05/2026
Yesterday we had an all company meeting and I thought it would be cool to kick ...
Read More
-
07/05/2026
Planning a tenant-to-tenant migration? Talk to us
Read More
-
31/03/2026
In tenant-to-tenant (T2T) and Google-to-Microsoft 365 migration projects, ...
Read More
-
27/03/2026
You’d think this would be easy and not an uncommon request. There are genuine ...
Read More
-
23/03/2026
Managing Google to Microsoft Migration with GAM7 and PowerShell
Read More
-
16/03/2026
When a user leaves an organisation in Microsoft 365, administrators have ...
Read MoreSubscribe to our newsletter for the latest updates and insights.
Like what you see? Stay in touch! Subscribers to our email list are among the first to receive the latest news, views and updates from Nero Blanco
Deleting Folders - Nero Blanco IT Migration Specialists">