Home  >  Exchange Server  >  Defrag Exchange Mailbox Database using PowerShell Utility

Defrag Exchange Mailbox Database using PowerShell Utility

Ravi Singh  | Updated on November 23, 2020 | Exchange Server   | No Comments   | 4 min read

Defragmentation is a process, on any data type source, to clean all unused storage from the database. In Microsoft Exchange Server and other Microsoft products, the databases increase in size with increased demand. However, if some data is deleted from the databases, the storage is not reclaimed. So, it would be good for the actual data access and performance that the defragmentation of the data is executed.

Let’s start by checking if the databases in Exchange Server need defragmentation. To do so, you need to use the Get-MailboxDatabase PowerShell cmdlet, as given below. This will give the database name, the actual size, and the available size.

Get-MailboxDatabase -Status | ft name, databasesize, availablenewmailboxspace -auto
get mailbox database status

As you can see from the above example, the Mailbox Database 0954395982 has a size of 247.9 MB. On the other hand, it also has 74.34 MB of white space. Of course, this is a small database with a few mailboxes in it. But on larger databases, defrag will make significant performance improvements on the disks and operation of the server.

From the performance point of view, the database access will be easier and the Exchange Server would not need to process a large file in memory or open and search the file which will incur a good number of input/output from the storage for nothing.

Another thing is the operation of the server. As you may know, Exchange Server is sensitive about the database’s health, if there is no storage or a shortage of it. Databases tend to fill up the hard drive either with unused space or the log files which will not be purged due to an issue. This might cause a lot of trouble, apart from the users not being able to receive or send emails. It can also damage or corrupt the databases.

So, it would be wise to set a schedule to check the databases state periodically, depending on the size and number of users. A corrupted database can cause loss of data, service, and business.

What is Needed to Defrag a Database?

To start off, before running any commands, it must be noted that defrag should be executed after office hours as it might take some time to finish (depending on the size of database and performance of the server/storage). Also, to defrag an Exchange Server database, the database must be dismounted. This means that the users will be unable to access their mailbox during this operation.

If you are short on storage capacity, this could cause a problem. During defragmentation, a new file is created and data is shifted, including the temp files. So, in a way, you would need extra space which will be equivalent to the database when it is defragmented. A formula to calculate the space is as follows:

Size of the database – space available X 1.1

Let’s take the example, discussed above, of the database having a size of 247.9 MB and 74.34 MB of free space. If we round off it to 250 MB and 75 MB respectively, then the total would be as follows,

250 – 75 = 175
175 X 1.1 = 192.5

In this case, you would need an extra 192.5 MB of free space to execute the defrag process.

Lastly, it is always recommended to back up the database before defragmentation. The reason being is anything can happen, even if this process is a normal process.

The Defrag Process

Follow the given steps to defragment the database:

Step 1: Open the Exchange Management Shell.

exchange management shell

Step 2: Go to the folder where the database that needs defragmentation is located.

defragment database

Step 3: Now, dismount the database by using the below command:

Dismount-Database <database name>
dismount database

Step 4: Once the database is dismounted, use the below EseUtil command to start the defragmentation process:

Eseutil /d "Mailbox Database 0954395982.edb" /t "temp_0954395982.edb"
run database defragmentation task

Step 5: Now, after defragmentation (which may take time depending on the performance of the server/storage and the size of the database), mount the database back by using the Mount-Database PowerShell cmdlet.

Mount-Database "Mailbox Database 0954395982"
mount database

Step 6: After mounting the database and testing connectivity of the affected users, you can test how the database has defragmented by using the Get-MaiboxDatabase PowerShell cmdlet.

Get-MailboxDatabase -Status | ft name, databasesize, availablenewmailboxspace -auto

Here you can see that the database has shrunk from 247 MB to 56 MB of size.

check database status

To Conclude

All goes well with the defrag operation, if you have not incurred any issues during normal operation. There are several factors that may corrupt the database before and during the defrag process. If you have not calculated and not left enough space for the process to run, this could lead to the storage getting full and halt the server, with the chance of corrupting the database.

In such situations, for any Exchange Administrator, a third-party Exchange recovery tool such as Stellar Repair for Exchange comes in handy to save the database when a disaster strikes. With Stellar Repair for Exchange, you will be able to open any Exchange Database of any version, corrupted or damaged, and recover data to PST and other formats. With this software, you will also be able to export data from an offline EDB to a live database in your Exchange Server and also to an Office 365 tenant.