Defragmenting the Active Directory Database


The Active Directory database tends to become fragmented over time, just like any other database does. Windows Server 2008 performs behind-the-scenes maintenance tasks in an effort to keep the database healthy, and defragmented. Although the data within the database is regularly defragmented, the database itself has a tendency to increase in size over time. This is especially true if administrators periodically purge database records. For instance, I once saw a 5 GB Active Directory database that contained less than 1 GB of data, and over 4 GB of empty space. You can reclaim the space by performing an off-line defragmentation.

Before I get started, I want to mention that reclaiming disk space is pretty much the only reason for performing an off-line defragmentation of the Active Directory database. The online maintenance cycle keeps the database healthy in spite of any free space that may exist within it.

Performing the Defragmentation

Windows has allowed you to perform an off-line defragmentation of the Active Directory database ever since the Active Directory was first introduced with Windows 2000. Prior to the release of Windows Server 2008 though, off-line defragmentation had to be performed in Directory Service Restore Mode. That’s because that was the only way to take the Active Directory database off-line. In Windows Server 2008, the Active Directory is a service. Any time that you want to perform maintenance on the Active Directory database, you can taken off-line by simply stopping of the Active Directory Domain Service.

I recommend beginning the defragmentation process by performing a full system state backup. Odds are that nothing will go wrong, but it’s a good idea to have a backup in place just in case the defragmentation process were to damage the database.

Once you have completed the backup, open Windows Explorer and navigate to the C:\Windows\NTDS folder. The Active Directory database is stored in the NTDS.DIT file, as shown in Figure A. You should make note of the size of this file so that you can go back later on and figure out how much space you have reclaimed.

Figure A

The Active Directory database is stored in C:\Windows\NTDS in a file named NTDS.DIT.

Those of you who are familiar with Exchange Server might recognize some of the files shown in the figure above. The Active Directory uses an Extensible Storage Engine database (also known as an ESE database or a JET database) just like Exchange does.

Now, open the Service Control Manager, and stop the Active Directory Domain Services service. When you do, you will see a message telling you that a number of dependency services also need to be stopped. Click Yes to stop these additional services.

After the services have been stopped, open a Command Prompt window on the server, and enter the following commands:

Activate Instance NTDS

At this point, you should see a summary of the files that are used by the Active directory database. You can see an example of this in Figure B.

Figure B
The Info command provides you with a summary of the server’s disk space and of the amount of space used by the Active Directory database.

If you have made it this far, then you are ready to begin the defragmentation process. To do so, enter the following command:

Compact to c:\windows\ntds\defragged

Windows should now begin the defragmentation process, as shown in Figure C. Keep in mind that depending on the size of your database, this process can take quite a while to complete, and the domain controller that you are defragmenting is unavailable until the Active Directory Domain Services and all of the dependency services are brought back online. Those of you who are used to working with Exchange Server will probably recognize the defragmentation process.

Figure C

This is what the actual defragmentation process looks like.

When the process completes, go to the C:\Windows\NTDS folder and rename the NTDS.DIT file to NTDS.OLD. You can delete this file later on, but hang onto it for right now just in case anything were to go wrong with the defragmented copy of the database. Now, copy the defragmented database from C:\Windows\NTDS\Defragged to C:\Windows\NTDS

Finally, restart the Active Directory Domain Services (the dependency services will restart automatically). When the process completes, you are all done!


Performing an offline defragmentation of the Active Directory database isn’t something that you should ordinarily have to do. It is a great way to reclaim some lost disk space should the system volume begin running low though.