Using Exchange EWS To Clear Purges Folder

This instalment of EWS fun is brought to you by Daya Patil, a Premier Field Engineer who specialises in Messaging.

Recently one of my customers came across a scenario when migrating from Exchange 2010  to Office 365.  In this issue deleted items were  being moved to the user’s online archive.  This caused significant delay and overhead to the move process since it was happening for several hundred mailboxes.  We found that many of the archive mailboxes were bloated with thousands of deleted items in the purges folder.  Get-MailboxStatistics showed the below results

Get-MailboxStatistics | Format-List

ItemCount : 23894

AssociatedItemCount : 339

DeletedItemCount : 6675

TotalDeletedItemSize : 95.54 MB (100,184,246 bytes)

TotalItemSize : 1.21 GB (1,299,032,376 bytes)


Get-MailboxStatistics –archive | Format-List

ItemCount : 122821

AssociatedItemCount : 1

DeletedItemCount : 250910

TotalDeletedItemSize : 1.107 GB (1,188,842,806 bytes)

TotalItemSize : 8.024 GB (8,615,913,596 bytes)



Migration of these items took a long time, and this customer wanted to remove those mails from archive dumpster.


Attempt # 1 – PowerShell

Clearing the Dumpster via PowerShell did not remediate the issue.

Using Exchange Management PowerShell, we tried deleting the mails from dumpster but no luck:

Get-Mailbox –Archive | Search-Mailbox –SearchDumpsterOnly –DeleteContent


Attempt #2 – Updated Policy Tag

Next we created a retention policy tag, assigned the retention policy tag to a dedicated retention policy and assigned this new retention policy to the desired mailbox. Unfortunately, disabling single item recovery and forcing Managed Folder Assistant (MFA) for the mailboxes had no effect on the contents of the online archive dumpster.

in Exchange 2010 the dumpster contents are preserved when moving mailboxes so there is no point moving it to a new mailbox database on-premises. 


Attempt #3  - MFCMAPI

Using MFCMAPI to delete the content worked. Unfortunately, this solution is inadequate for a bulk amount of several archive mailboxes.


Attempt #4 – EWS

Since Exchange Web Services provides a very powerful method to interact with Exchange, can we not use that?  Yes we can! 

EWS worked well to remediate this issue.  The sample code is provided below. 



EWS Sample Code

Please note that is is sample code.  Use this at your own risk, and must be validated in your test lab prior to executing in production. 



To use Exchange Web services managed API 2.0  with PowerShell, you need to first download it from here.

## To Load Exchange Web services managed API DLL

Import-module "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll" 

# Set Exchange Version 

$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1

$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value

$user = [ADSI]"LDAP://<SID=$sid>"

## Create Exchange Service Object

$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion) 



# List affected mailbox in a variable. Here you can use a variable to store list of all mailboxes using Get-Content and then use ForEach loop

$mailboxname = ""

# Connecting to Recoverable item folder of Archive mailbox. The list of all folders to which you can connect using EWS is available here.

$rfRootFolderID = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]:: ArchiveRecoverableItemsRoot,$MailboxName)

$rfRootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$rfRootFolderID)

# Setting scope for EWS to process items

$fvFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(10000);

$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep

# Finding Sub folders in Recoverable items root of Archive mailbox (Deleted Items, Purges, Versions)

$ffResponse = $rfRootFolder.FindFolders($fvFolderView)

# Empty each folder

foreach ($folder in $ffResponse.Folders)


$Folder.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete, $true)




Again -- Please test it thoroughly before using it in your production environment.




This post is brought to you in a base64 encoded format by MSPFE editor Rhoderick Milne

Comments (2)

  1. Alex says:


    I’ve been trying to use an adaptation of your script but I’ve been having trouble when it comes to running the Clean command on the purges folder: after a few seconds of thinking, I get a ServiceResponseException saying “The folder can’t be emptied”.

    I only get this error on the Purges folder… sometimes it does work without any error, but that’s maybe 5% of the times?

    Any idea what it might be?

    1. Daya says:

      There are two cases where this can happen. If SingleItemRetention is enabled or if a hold is in place for the mailbox (either Litigation or InPlace that includes items in the Purges folder), then you won’t be able to empty the Purges folder.

Skip to main content