O-Xchange Notes from the Field!

Thursday, January 28, 2016

Use EWS to clean recoverable items folder

Sometimes, you may have the need to clean out recoverable items. We have seen users who have over 90,000 items in the recoverable folders. For details on cleaning up recoverable items folder:

read this:https://technet.microsoft.com/en-us/library/ff678798(v=exchg.150).aspx

The commands below can be used to remove items from recoverable items folder.

When you use search-mailbox to delete recovery items folder (see the powershell commands below)

The commands below deletes all items from deletions folder(under recoverable items folder)

cmdlet #1:
Search-Mailbox -Identity "test" -SearchDumpsterOnly -DeleteContent

Cmdlet #2:
Search-Mailbox -Identity "test" -SearchDumpsterOnly -TargetMailbox "Discovery Search Mailbox" -TargetFolder "test-RecoverableItems" -DeleteContent

This example permanently deletes items from test mailbox Recoverable Items folder and also copies the items to the test-RecoverableItems folder in the Discovery Search Mailbox (a discovery mailbox created by Exchange Setup).

Those commands work but only delete all items from deletions folder(under recoverable items folder). The deleted items are moved to Purges folder.

In my opinion, the best way to delete recoverable items is to delete items from recoverable items root using ews.
Use the script below to delete items from recoverable items folder. You will need to modify script with your cas server fqdn, username, password, domain name and of course the mailbox name you are removing items for.  Copy to notepad and Save as Ps1 file, then run from exchange PowerShell.

Note: This script will delete all items from recoverable items folder root(deletions, versions, purges). Please test before deploying in production.

start script

## Load Managed API dll
Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
## Create Exchange Service Object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
## Set Credentials
$casserverName = "https://server fqdn"
$userName = "impersonation"
$password =  "impersonate"
$domain = "domain name"
$service.Credentials = New-Object  System.Net.NetworkCredential($username,$password,$domain)
## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use
$MailboxName = "test@test.com"
#CAS URL Autodiscover
## Set the Folder, and bind to it
$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Recoverableitemsroot,$MailboxName)
$tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)

if($tfTargetFolder -ne $null){
    #Empty the Folder using HardDelete
    $tfTargetFolder.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete, $true)

End script

No comments:

Post a Comment