What is Compact and Repair (C&R)?
What does it do exactly?
Why do most experience developers recommend it so much?
Well, these are some of the questions that I am going to try to answer in this article. So let’s take a closer look at Microsoft Access’ Compact and Repair command.
So What Does the Compact and Repair Command do?
One of the best explanations I found many years ago was by MVP David Fenton:
…
A compact organizes everything neatly and gets rid of all the slack space. It also rewrites data tables in primary key order (Jet/ACE clusters on the PK, but that’s the only index you can cluster on). Indexes are also rewritten at that point, since over time those become fragmented with use, also.David Fenton
If a primary key exists in the table, compacting re-stores table records into their Primary Key order. This provides the equivalent of Non-maintained Clustered Indexes, and makes the read-ahead capabilities of the Microsoft Jet database engine much more efficient.
Compacting also updates the table statistics within the database that are used as Jet optimizes queries. These statistics can become outdated as data is added, manipulated, and deleted from the various tables. Query speed will be enhanced significantly, because they are now working with data that has been rewritten to the tables in contiguous pages. Scanning sequential pages is much faster than scanning fragmented pages. Queries are forced to recompile/optimize after each database compaction.Microsoft
Okay, I know, that’s all very technical stuff. In plain English, it translates into things like:
Compact and Repair is like performing a spring cleaning on your database. It tidies things up, reorganizes the inners and declutters everything, takes the trash out… So in the case of an Access database that equate to removing old data, and/or objects, that were deleted, rearranges the data in an optimal manner, …
Ultimately Compact and Repair :
- Reduces the overall file size
- Improves performance and may resolve/correct weird and erroneous behaviors (queries, forms/reports, VBA)
- Resets AutoNumber to the next value in the sequence (eliminating gaps that may have been occurring)
Recovering Content
Before going any further, I think it is important at this point to cover one important point regarding database Compact and Repair.
As mentioned above, Compact and Repair clear out data, declutters, from the database file. Thus, should you accidentally delete record, database objects, … and wish to recover them (there are ways to do so) it is CRITICAL that you NOT perform a Compact and Repair or else it (whatever you are hoping to recover) will be purged and gone forever!
Basic Requirements
To perform a Compact and Repair there is truly only a single requirement and that is that no one be using the database. Unlike many other ‘enterprise’ databases, Access cannot perform a Compact and Repair on a database which is in use. Instead, everyone must exit the database as the process requires exclusive access to run.
If you try to Compact a database which is in use, you will receive and error such as:
in such cases you will need to ask the user(s) to close the database and then try again to perform the database compaction.
How Does It Work?
Basically, Compact and Repair makes another ‘optimized’ database from the original database and then copies the ‘optimized’ version over the old version. This becomes evident when you try to perform a Compact and Repair via VBA code as it is a multi-step process in which one must finish by deleting the existing db and renaming the compacted one.
This is why the database actually briefly close and reopens when you perform a C&R.
Some Best Practices
Below are what I consider Best Practices to follow with regards to performing Compact and Repairs on Microsoft Access databases.
Backup
I highly recommend that you always make a backup prior to compacting, to protect against potential file corruption. It doesn’t happen frequently, but every once and a while you see reported that someone performed a Compact and something in the db is now corrupted. A simple copy/paste (backup) provides a quick and simple fix for such occurrences.
Local Please
Do not perform over a network!
Always be sure to copy the file to your local PC, compact the file, the copy it back to the server.
Performing a Compact and Repair over a network connection risks corrupting the file and it typically slower than doing so locally. In some environments the difference in speed of execution is phenomenal!
Don’t Do It!
Do not use the Compact on Close database option. Because of potential the risk of corruption associated with the Compact and Repair process. I advise against blindly compacting a database every time it is closed.
First and foremost, a database should not require such frequent compaction and if it does it is indicative of design issues that should be addressed properly rather than using Compact on Close as a BandAid to mask the issue.
Secondly, I believe you should always remain in control of your database and this also mean being in control of how and when maintenance is performed! Thus, Compaction should be done in a planned manner, and backups perform prior (something Compact on Close does not do!).
Automation You Say
One beautiful thing about the Compact command is the fact that it can be scripted via batch file, VBScript, PowerShell… Using simple command line switches, but, as mentioned above, be sure to perform a backup prior to compacting.
You can learn more about Access Command Line Switches in my article:

If you wish to automate this process via VBA then I urge you to review:

When To Compact a Database
Typically, front-end need compacting during development and shouldn’t really require compaction when in production. If the front-end bloats regularly then this is indicative of design issues and should be addressed.
Furthermore, since a front-end should be compiled and compacted prior to production release, further compaction by user should not normally be required. Moreover, a great way to eliminate the need altogether is to use a launch tool/script that automatically provides a fresh new copy of the front-end to the user every time they launch the database. This ensures they always have a pristine copy to work with and eliminates all potentially required maintenance.
The back-end ‘may‘ require compaction, but even that should be minimal in a properly designed system.
That said, whenever I take over a project or am asked for assistance with a database exhibiting weird behaviors … I usually perform a Compact and Repair (after having made a backup copy first). It is also something I perform (on the back-end) whenever I visit my clients’, just as a quick maintenance to ensure things are optimal.
How Much Improvement Will I See
That’s a great question and it will vary greatly.
Some databases that are well designed, or used very seldom may see little to no improvements. Yet, other databases may see HUGE improvements by a simple Compact and Repair. It all depends on things like:
- How long it’s been since the last compaction
- How much use the database gets
- How well the database was designed
- The types of operations frequently done in the database (Insertion/Deletion will see improvements)
- … (refer to my bloating article)
So there is no way to know for sure if your database will benefit, but it cost nothing to try!
For what it’s worth, one thing you can do however, from a size perspective, is simply compare the file size of the backup copy to the compacted database to get an idea of the reclaimed storage space.
A New Database!
The last thing I’d like to mention about Compaction is that sometimes even it isn’t enough/perfect/optimal. Sometimes you are best to quickly create a new blank database and simply import all the objects from your current database. Then set the necessary VBA references, compile your VBA code and perform a Compact and Repair.
This is a great thing to do before official distribution into production of any database.
Further Readings
Now that you’ve got a better understanding about the Compact and Repair command, the next step would be to understand what causes a Microsoft Access database to grow, or what is more commonly referred to a database bloating. Thus I would highly recommend you continue your learning journey by reading:
and here’s my original article on Compacting a database:

Other Resources on the Subject
