During your testing, 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 firstname.lastname@example.org | Select FolderPath,ItemsInFolder
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:
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.