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://video2.skills-academy.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!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,254 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,343 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
525 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,465 questions
0 comments No comments
{count} votes

Accepted answer

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('first.last@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 - first.last@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