Active Directory automatically performs online defragmentation of the database at certain intervals as part of the Garbage Collection process. (By default, this occurs every 12 hours.) Online defragmentation does not reduce the size of the database file (Ntds.dit) but instead optimizes data storage in the database and reclaims space in the directory for new objects.
How to Perform Active Directory Offline Defragmentation?
It’s important to defragment the Active Directory database for best performance. Normally there is an online defragmentation that occurs about twice a day on the database, but this is more of a backup than defrag. It can give admins a false sense of security, thinking that the database has been defragged without any downtime.
The only way to truly defragment the Active Directory database, remove whitespace and decrease its size is with an offline defrag. This requires you to take Active Directory offline by booting a DC into Directory Service Restore Mode (DSRM), which boots up the DC in safe mode without mounting the AD database. Once booted, enter the NTDSUtil program and you will be able to perform a number of actions that are not possible with Active Directory online.
After the services have been stopped, open a Command Prompt on the server, and enter the following commands:
To begin the defragmentation process, enter the following command:
- Activate Instance NTDS
- Compact to C:\Temp\ntds-db
When the process completes, you need to do what it says and copy the defragged database from C:\Temp\ntds-db\ntds.dit to C:\Windows\ntds\ntds.dit.
If you want to play with this in a test domain, you can record the size of the NTDS.DIT and then create a large number of users — say 10,000 — in the domain. Note the increased size of the NTDS.DIT. Then delete the users and follow the procedure just described to compact the database and replace the old one with the whitespace for the 10,000 deleted users. Compare the size of the new, compacted database with the size after you created the users. You could also wait for an online defrag of the database to occur (recorded with an event in the system event log) and see if that changes the NTDS.DIT size. Note that in a production situation, you want to perform database integrity checks to ensure stability.
It is not necessary to perform this offline defrag on a regular basis, but it’s good to do it after significant changes have been made — such as the removal of a large number of users or groups — to keep the database at an efficient size.