How to delete Exchange folder using Powershell or otherwise

GM 136 Reputation points
2022-10-21T19:29:45.89+00:00

One of our test domain M365 accounts has accumulated a large number of items in the \Deleted Items Exchange Online Folder. From OWA Trash and Recoverable Items for the user are clean.

This seems to be causing most of the Exchange APIs we are using to grind to a halt.

I'm looking for some advice on how to actually clean-up not just the items but also the folders in \Deleted Items. For removing the items we have been able to adapt some of the instructions in https://learn.microsoft.com/en-us/exchange/policy-and-compliance/recoverable-items-folder/clean-up-deleted-items?view=exchserver-2019 to clean-up items in folders that are also under Deleted Items. We have also tried the Graph API, but it is not able to see any of these folders.

We are at a loss on how to delete the remaining folders which are now empty.

Thank you for your help!

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,171 questions
Exchange Exchange Server Development
Exchange Exchange Server Management
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. Andy David - MVP 157.4K Reputation points MVP Volunteer Moderator
    2022-10-21T19:56:07.747+00:00
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. GM 136 Reputation points
    2022-10-22T00:54:36.193+00:00

    Thank you @Andy David - MVP

    The first link - https://community.spiceworks.com/topic/2181078-powershell-delete-a-folder-from-all-mailboxes-exchange-online-o365 was helpful. Once I adapted the script to use OAuth (EWS basic auth is now disabled), I was able to get that to work. Folders are now deleting.

    Did not try the other suggestions since they required local binaries that I could only get on a Windows machine.


  2. RM Haley 11 Reputation points
    2023-07-21T17:16:30.67+00:00
    Add-type -assembly "Microsoft.Office.Interop.Outlook" | Out-Null
    $oLFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
    $outlook   = New-Object -ComObject Outlook.Application
    $namespace = $outlook.GetNamespace("MAPI")
    $deleted   = $namespace.Folders.Item('******@domain.com').Folders.Item("Deleted Items")
    cls
    while(1){
        $deleted.Folders | %{
            Write-Host $_.Name -fore Magenta
            $_.Folders | %{
                Write-Host "	"$_.Name -fore Cyan
                $_.Delete()
            }
            $_.Delete()
        }
        cls
    }
    
    

    I am using this, and it's quite effective.
    I am also pointing this to the Archive, which is "Online Archive - ******@domain.com"


  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.