When you create and delete a large number of test mailboxes over and over again in a pilot phase of a project, you may want to clean up the Mailbox Database before going into production.

With the clean-database powershell command you are able to cleanup the mailbox database. The command will purge all mailboxes that have an expired retention date.  When you run the command clean-mailboxdatabase in the Exchange Management Shell without setting the retention time you may receive the following event in your Application eventlog.

Event 9535: 
0 deleted mailboxes (0 kb) have been removed 
100 deleted mailboxes (2348382 kb) have been retained

By default the retention date is set to 30 days and it is set on the mailbox database. When you would like to purge mailboxes deleted for example today, you need to set the retention time on zero days. You can do this by access the Exchange Management Console or executing the command below in the Exchange Management Shell

set-mailboxdatabase -Identity "Mailbox Database" -MailboxRetention 00.00:00:00

To clean up the mailbox database you need to run the clean-mailboxdatabase command.

clean-mailboxdatabase -identity "Mailbox Database"

In the event log of the machine you will see

Event 9535: 
100 deleted mailboxes (2348382 kb) have been removed
0 deleted mailboxes (0 kb) have been retained

Next step is to compact and defrag the mailbox database, this is done by dismounting the mailbox database and use the esutil.exe utility to defrag the database.

  1. Start a command line and go to the directory where the mailbox database resides
  2. Start eseutil /d "Mailbox Database.edb"
  3. Mount the database.

When you are finished you may want to set the retention time of the deleted mailboxes back to the original setting by executing the command below:

set-mailboxdatabase -Identity "Mailbox Database" -MailboxRetention 30.00:00:00

After all of this you have a “clean” database and you can transfer your project to the next phase.