Can I ‘Shrink’ My Exchange Database?

KB ID 0001706

Problem

Saw this asked in a forum today, and my response was;

It never gets smaller, if you delete things out of it, it just creates more ‘whitespace’ within the database, unless you;

a) Take it offline, and defragment it using eseutil.
b) Move the mailboxes out of it, to another database and delete it.

Which is true, but even Microsoft say you should not need to defragment a database! Before you do anything make sure you have a good backup of Exchange and the mailbox databases!

Defragment an Exchange Database

Stop! Why are you doing this, if it’s because you are running out of room, then migrating mailboxes out of a database into another database, on another piece of storage with more room is what you should be doing, and does not involve long periods of downtime!

If you have just deleted hundreds of GB from a mailbox database, and you simply want to compress the DB, and you have a BIG window for downtime, (allow 1 hour for every 9GB of database size). Then you are in about the only use case scenario I can think of to want to do this!

Before You start: This process creates a temporary mailbox database, (you need to tell it where), if you are pressed for room, I suggest you add another volume/drive and put it there. (Though in my example I have room in the same folder). Allow for the entire database’s existing size plus ten percent to be on the safe side.

Let’s have a look, and see how  much room we might be able to reclaim (whitespace);

[box]

Get-MailboxDatabase -Status | Select Name, DatabaseSize, AvailableNewMailboxSpace

[/box]

The database has to be dismounted before you can run Eseutil on it (downtime starts here).

[box]

Dismount –Database Database Name

Eseutil /d C:\Folder\DatabaseName.edb> /t T:\Folder\TempDB.edb>"

[/box]

When complete, mount the datastore again, (downtime ends here).

[box]

Mount-Database Database Name

[/box]

Hopefully you should see the whitespace has decreased.

Don’t forget to set off a full backup of the store as soon at it’s back online.

Move Mailboxes To Another Exchange Database (To Reclaim Space)

This is the much more elegant solution, create a shiny new database move everything  from the old database into the new one, and delete the old database.

Will This Impact My Users? Any user using their mailbox while it is getting moved wont be affected, until the move is complete then they will see a popup that looks like;

The Microsoft Exchange administrator has made a change that requires you quit and restart Outlook.

Cool eh! So lets start by creating a New Mailbox Database.

[box]

New-MailboxDatabase -Name New Database Name -EdbFilePath C:\Folder\DB-Name.edb -LogFolderPath C:\Folder\Folder\

[/box]

Make sure you follow the advice and restart the information store, (yes you can mount the new DB and proceed, but Exchange does not allocate the resources correctly if you do this).

[box]

Restart-Service MSExchangeIS

[/box]

Let’s move our ‘Normal’ mailboxes to the new DB.

[box]

Get-Mailbox -Resultsize Unlimited | New-MoveRequest -TargetDatabase Target Database Name

[/box]

Depending on how many (and how large) mailboxes there are this can take a while, (days for large databases!) When they’ve all completed you need to remove the  move requests, (if you have any failures, or speed issues use the search box above, I’ve had to cope with thousands of these things not working correctly in my time!)

Let’s assume we are all moved, so we want to remove the move requests.

[box]

Get-MoveRequest -MoveStatus Completed | Remove-MoveRequest

[/box]

Now let’s see if we have any Arbitration Mailboxes, we do so let’s shift them;

[box]

Get-Mailbox -Database Source Database Name -Arbitration

Get-Mailbox -Database Source Database Name -Arbitration | New-MoveRequest -TargetDatabase Target Database Name

[/box]

Again, when complete, remove the move requests.

[box]

Get-MoveRequest -MoveStatus Completed | Remove-MoveRequest

[/box]

Now let’s see if we have any Auditlog Mailboxes, we do so let’s shift them;

[box]

Get-Mailbox -Database Source Database Name -Auditlog

Get-Mailbox -Database Source Database Name -Auditlog | New-MoveRequest -TargetDatabase Target Database Name

Get-MoveRequest | Get-MoveRequestStatistics

Get-MoveRequest -MoveStatus Completed | Remove-MoveRequest

[/box]

Now let’s see if we have any Archive Mailboxes, we do NOT so I will skip them, let’s check for Monitoring Mailboxes, we have those so let’s move them;

[box]

Get-Mailbox -Database Source Database Name -Archive

Get-Mailbox -Database Source Database Name -Monitoring

Get-Mailbox -Database Source Database Name -Monitoring | New-MoveRequest -TargetDatabase Target Database Name

[/box]

And clean those up as before.

[box]

Get-MoveRequest | Get-MoveRequestStatistics

Get-MoveRequest -MoveStatus Completed | Remove-MoveRequest

[/box]

Finally let’s see if we have any Public Folder Mailboxes, we have one of those so let’s move that;

[box]

Get-Mailbox -Database Source Database Name -PublicFolder

Get-Mailbox -Database Source Database Name -PublicFolder | New-MoveRequest -TargetDatabase Target Database Name
[/box]

We now need to remove the ‘Source‘ Database but first we need to dismount it.

[box]

Dismount-Database OLD Database Name

Remove-MailboxDatabase -Identity OLD Database Name

[/box]

I get an error message saying I cant delete the database.

 

Related Articles, References, Credits, or External Links

NA