While migrating to Microsoft 365 (Office 365), it might be useful to be able to empty a mailbox to perform several tests.
This article explains how to empty a mailbox in Office365 or Exchange using PowerShell.
How to delete mailbox content in Office 365 or Exchange
There is a useful command in PowerShell (search-Mailbox -DeleteContent)
This cmdLet is available by default in Exchange OnPremises.
However, an additional step is necessary in Office365 to enable this cmdLet: By default, it’s not exposed because the “Mailbox Import Export” role is not set.
1. Enable “Mailbox Import Export” role
- Connect to Office365 Portal.
- Select Admin -> Exchange.
- Click on Permissions.
- Double-click on Organization Management.
- Under Roles, click on the plus sign (+).
- Add the Mailbox Import/Export role.
- Then ensure that your account is a member of the Organization Management.
1.2 Add permissions with Powershell
you can also use the following cmdlet to add the described above permissions:
New-ManagementRoleAssignment -Name "Import Export Enterprise Support" ` -User "[Admin Identity]" ` -Role "Mailbox Import Export"
2. Delete the content of a mailbox
This will delete all content (mails, contacts, calendars).
To verify, you can issue the command :
Get-MailboxFolderStatistics -Identity john.doe@company.com | Select FolderPath,ItemsInFolder
Example:
Search-Mailbox -Identity "John Doe" -DeleteContent
Note: You will notice that it does not delete personal folders.
Deleting folders needs an additional step.
2.1 Delete the Calendar folder of a mailbox
This will delete only calendar entries in user’s mailbox.
Search-Mailbox -Identity "John Doe" -SearchQuery "kind:meetings" -DeleteContent
Moreover, check this thread: it provides a small script for deleting folders:
http://social.technet.microsoft.com/Forums/windowsserver/en-US/bc07df23-64f1-4732-a399-76fd39b1595e/powershell-exchange-2010-delete-folder-in-mailbox-or-delete-mail-in-certain-folder?forum=winserverpowershell
IMPORTANT
Customers use the scripts at their own risk.
Cloudiway will provide no support for script use. Cloudiway provides no guarantee that executing the script will result in data recovery.