Delete the contents of a user mailbox in Exchange and Microsoft 365
Problem:
You want to delete the mailbox contents of On-Premises Exchange and Exchange online user
Solution:
You can use PowerShell commands to delete the mailbox contents in Exchange Server 2019, 2016, 2013, 2010 or Exchange Online (Office 365). You must set the required permissions before you can run the cmdlets. Let’s understand how to:
Set the permissions to delete mailbox contents in the Exchange admin center
On-Premises Exchange server
- Log in to the Exchange Admin Center
- Navigate through 'Permissions' > 'admin roles'
- Select 'Organization Management' and click the 'Edit (Pencil)' icon.
- Click '+' under 'Roles'
- Select the following roles and add:
- Mailbox Import Export
- Mailbox Search
- Click 'OK' and 'Save' the changes
Exchange Online (Microsoft 365)
Set the permissions to delete mailbox contents using PowerShell
Run the following command to assign the 'Mailbox Import Export' role :
New-ManagementRoleAssignment –Role "Mailbox Import Export" –User "username"
Here, 'username' is the User Principal Name (UPN).
You may have to reconnect to Exchange using PowerShell to reflect the permission changes. Either relaunch the PowerShell console, or use Disconnect-ExchangeOnline and Connect-ExchangeOnline again.
Delete mailbox contents in Exchange Online
Delete mailbox contents by using Search-Mailbox (Exchange Online, Exchange 2019, Exchange 2016, Exchange 2013, Exchange 2010)
The official support for this approach ended on July 1, 2020. However, you can use the Search-Mailbox cmdlet to delete the mailbox content in Exchange online, although the method is not officially supported. The item limit per mailbox is 10,000.
Click here to know the steps for Exchange migration using EdbMails
Click here to know the steps for Exchange Online (Office 365) migration using EdbMails
- Log in to the Exchange Admin Center
- Navigate 'Roles' > 'Admin roles' and click the 'Organization Management' from the role group list.
- Select the 'Permissions' tab and choose the 'Mailbox Import Export' and 'Mailbox Search' roles
- Click 'Save' and close the window
- Start a remote connection with your Exchange Online environment using PowerShell. You need to connect to the Security and Compliance Center for Exchange Online
Connect-IPPSSession -UserPrincipalName <admin’s-UPN>
- Run the following command to search the mailbox contents and initiate the search
New-ComplianceSearch -name <search-name> -ExchangeLocation <mailbox-name> | Start-ComplianceSearch
- Run the following command to verify the status of the Search
Get-ComplianceSearch | FL name,items,size,jobprogress,status
- Run the following cmdlet to delete the mailbox contents
New-ComplianceSearchAction -SearchName <search name> -purge
Note that the New-ComplianceSearchAction can only delete 10 items at once from each mailbox. If you want to delete more items, you need to either use precision searching or repeat the process using a loop.
- Log in to the 'Exchange Management Shell'
- Run the following PowerShell command to verify the number of items that will be deleted:
Search-Mailbox -Identity "<MailboxOrMailUserIdParameter>" -EstimateResultOnly
Here,'MailboxOrMailUserIdParameter' can be an alias, canonical DN, display name, user principal name etc. Refer this Microsoft article for more details on the Search-Mailbox cmdlet.
- Run the following PowerShell command to delete the mailbox contents:
Search-Mailbox -Identity "<MailboxOrMailUserIdParameter>" -DeleteContent -force