Knowledge Base

How to delete the contents of a user mailbox

Problem:

You would like to delete the contents of a user's mailbox in Exchange Server or Exchange Online (Office 365).

Solution:

In Exchange Server 2019, 2016, 2013, 2010 or Exchange Online (Office 365), you can use PowerShell to remove mailbox contents. But before you can use specific cmdlets, you need to assign the necessary permissions. See below on how to:

Assigning permissions necessary to delete mailbox contents in the Exchange admin center

On-premises Exchange server

  1. Log in to the Exchange admin center.
  2. Go to permissions admin roles, select Organization Management, and click Edit (Fig. 1.).

Organization Management permissions
Fig. 1. Editing admin roles in the 
Exchange admin center. 

  1. ​Click the plus (+) button in the Roles section (Fig. 2.).

349-2
Fig. 2. Editing the roles of the Organization Management
role group.

  1. Select and add:
    • Mailbox Import Export
    • Mailbox Search (Fig. 3.)

349-3
Fig. 3. Adding additional roles to the role group in
the Exchange admin center.

  1. Click OK, Save and close the Exchange admin center. If you are using an on-premises Exchange server, log out of Windows and log back in.

Exchange Online (Office 365)

  1. Sign in to the Exchange admin center.
  2. Go to Roles > Admin roles and select Organization Management from the role group list.
  3. In the pane that opens, go to the Permissions tab and select the Mailbox Import Export and Mailbox Search roles (Fig. 4.).

Adding new roles to the Organization Management role group.
Fig. 4. Adding new roles to the Organization Management role group.

  1. Click Save to submit changes and close the pane.

Assigning permissions necessary to delete mailbox contents by using PowerShell

Run the following cmdlet to assign the Mailbox Import Export role (required to delete mailbox contents with PowerShell):

New-ManagementRoleAssignment –Role "Mailbox Import Export" –User "username"

where username is the User Principal Name of your account.

Deleting mailbox contents by using New-ComplianceSearch (Exchange Online, Exchange 2019 and 2016)

  1. Log in to the ems Exchange Management Shell or use PowerShell to start a remote connection with your Exchange environment. In Exchange Online, make sure to connect to the Security and Compliance Center:
$credential = get-credential;
$SccSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $credential -Authentication "Basic" -AllowRedirection;
Import-PSSession $SccSession
  1. Run the cmdlet below to search for mailbox contents and start the search:
New-ComplianceSearch -name <search-name> -ExchangeLocation <mailbox-name> | Start-ComplianceSearch
  1. (Optional) Check the status of your searches and how many items they returned:
Get-ComplianceSearch | FL name,items,size,jobprogress,status
  1. After the search is complete, delete the contents with the following cmdlet:
New-ComplianceSearchAction -SearchName <search name> -purge

Tip

If you want to learn more about this method of deleting content, and how to filter results, see this article.

Mind that New-ComplianceSearchAction deletes only 10 items per mailbox at a time. This means that you need to perform precision searching or reiterate using a loop if you need to delete more items.

Delete mailbox contents by using Search-Mailbox (Exchange Online, Exchange 2019, Exchange 2016, Exchange 2013, Exchange 2010)

At the moment of writing, this method still works for Exchange Online, but returns a warning that it is being retired and Microsoft Support will no longer provide assistance. Search-Mailbox is announced to become unavailable on July 1, 2020.

  1. Log in to the ems Exchange Management Shell
  2. (Optional) Run the PowerShell cmdlet below to check how many items will be deleted:

    Search-Mailbox -Identity "<MailboxOrMailUserIdParameter>" -EstimateResultOnly

    The MailboxOrMailUserIdParameter can be, for example, an alias, canonical DN, display name, user principal name or distinguished name. See this Microsoft article for further reference.

  3. Run the PowerShell cmdlet below to delete the mailbox contents:

    ​​Search-Mailbox -Identity "<MailboxOrMailUserIdParameter>" -DeleteContent -force

Tip

If you want to delete the content of multiple mailboxes or are looking for more options, you may find this article helpful.

Alternative solutions

Alternatively, to delete items from mailboxes on any on-premises Exchange servers, you can use a free third-party tool, MFCMAPI. Refer to the tool's website for more details.

Was this information useful?