Friday, May 25, 2012

Move mailbox to free up space in databases

Admins are from time to time doing mailbox moves. Reason could be several but the goal is often to create some white space in mailbox databases.

By moving mailboxes to a different database you think it will create "holes" in the source database, and that's true until you the source server is Exchange 2010 service pack 1. With Service Pack 1 the behavior changed to leave the data it the source database until it get cleaned up when the mailbox retention limit is reached.
Reason for the change is that mailbox data should be easily accessed in a catastrophic failure before having a proper backup of the target database.

But what if you really want to create some space in the database now?
You could change the mailbox retention timeout to low number of days or even zero days. This might not be suitable solution and then you have to clean up the left-overs manually.
Article above say that mailboxes is in a soft-deleted state so they can be found with this command.

Get-MailboxStatistics -Database <databasename> | where {$_.DisconnectReason -eq "SoftDeleted"}

To clean up, you use the cmdlet Remove-StoreMailbox.

To make it work you do this is to steps. First save the mailboxes you want to delete in a variable.

$mbxs = Get-MailboxStatistics -Database databasename_you_want_to_clean_up | where {$_.DisconnectReason -eq “SoftDeleted”}

and then you delete them

$mbxs | foreach {Remove-StoreMailbox -Database $_.database -Identity $_.mailboxguid -MailboxState SoftDeleted -Confirm:$false}

This will trigger a process to clean up in the database and you can follow it creating whitespace with this command

Get-MailboxDatabase -Status | Sort-Object name | Format-Table Name, DatabaseSize, AvailableNewMailboxSpace

or for a specific database

Get-MailboxDatabase -Identity databasename -Status | Format-Table Name, DatabaseSize, AvailableNewMailboxSpace


Remove-StoreMailbox is the same command used for purging deleted mailboxes.

$mbxs = Get-MailboxStatistics -Database databasename_you_want_to_clean_up| where {$_.DisconnectReason -eq “Disabled”}

and then

$mbxs | foreach {Remove-StoreMailbox -Database $_.database -Identity $_.mailboxguid -MailboxState Disabled -Confirm:$false}

After you purged disabled or softdeleted mailboxes you should have created some whitespace for mailboxes to grow in without growing the EDB file.

Happy purging.