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.
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.
Delete the content of a mailbox
Run: Search-Mailbox -id -DeleteContent -force
example: Search-Mailbox -id vuser1 -DeleteContent -force
This will delete all content (mails, contacts, calendars).
To verify, you can issue the command get-mailboxfolderstatistics | select FolderPath,ItemsInFolder
Note: You will notice that it does not delete personal folders.
Deleting folders needs an additional step.
Check this thread: it provides a small script for deleting folders: