Short answer: figure out which database engine and file you actually have first, because "compact and repair" means different things in Microsoft Access, SQL Server, and SQLite. In Access, Compact and Repair is a built-in maintenance command. In SQL Server, there is no single compact-and-repair button, so you rebuild or reorganize indexes, shrink only if space is truly a problem, and fix corruption with DBCC CHECKDB and a restore. In SQLite, the equivalent pair is VACUUM and integrity checking. The rest of this guide walks each case in order, with stop conditions.
The confusion is common because the phrase "compact and repair" comes from desktop database culture, while server engines split those jobs into separate operations that carry different risks. If you shrink a SQL Server database to "repair" it, you can actually slow it down. If you open a SQLite file in Access, you'll get errors that look like corruption but aren't. So the first five minutes should go to identification, not to running anything.
Identify the engine and file before touching anything
Compacting and repairing is maintenance, and maintenance done against the wrong target is how small problems become outages. Work through these checks before you run a single command.
1: Look at the file extension. .accdb and .mdb are Access. .mdf and .ldf are SQL Server data and log files. .db, .sqlite, and .sqlite3 are almost always SQLite. A .bak is a backup, not a live database. 2: Confirm how the app connects. A program using an ODBC or OLE DB connection string that names a SQL Server instance is not an Access file, even if a user calls it "the database." Check the connection string or the app's configuration, not the icon on the desktop. 3: Check for a server process. SQL Server runs as a Windows service and holds its files open exclusively, so you can't copy or compact a live .mdf the way you can a desktop file. SQLite is the opposite: it's a library, and the file is just a file. 4: Find out whether anyone else is using it. For Access, that means a shared network folder with a locking file (.laccdb or .ldb). For SQLite, it means concurrent writers. For SQL Server, it means active sessions and jobs. 5: Take a backup first, always. For file-based databases, copy the file while no one is writing to it; a copy taken mid-write can itself be inconsistent. For SQL Server, use a native backup or a verified copy of a clean backup set.
Success check: you can state the engine, the exact file path, the number of current users, and where your backup lives. If any of those is unknown, stop and resolve it before proceeding. That is the first real stop condition, and it's reversible: nothing has changed yet.
Compact and Repair in Microsoft Access, step by step
Access is the only mainstream product where "Compact and Repair" is literally a menu command, and it does two jobs at once: it rewrites the file to reclaim space left by deleted objects and records, and it rebuilds internal structures that can drift over time. That's why Microsoft's guidance treats it as routine maintenance rather than a last resort, especially for front-end files that get heavy use.
Prerequisites: exclusive access (no one else in the file), a backup copy, and enough free disk space to hold roughly twice the file size, because Access writes a new file and swaps it in.
1: Close the database and confirm no one else has it open. The locking file should disappear when the last user exits. On a shared drive, ask users to close it rather than guessing. 2: Make a backup copy of the .accdb or .mdb file to a different folder or drive. Don't skip this because the operation is "routine"; a repair against a damaged file can fail partway. 3: Reopen the file in Access and go to File > Info > Compact & Repair Database (in older ribbon versions it's under Database Tools). If you're compacting the file you currently have open, Access handles the close-and-reopen for you. 4: For a closed file, use Database Tools > Compact and Repair Database, then select the file. Access compacts it and reopens the result. 5: Verify afterward. Open the main forms and reports, run a couple of representative queries, and check that linked tables still resolve. Note the new file size; a meaningful drop is normal after heavy deletions.
What can go wrong, and what to do: if Access reports that the database is in an unrecognized format or can't be repaired, don't run Compact and Repair repeatedly on the same file. Restore your backup instead, and consider whether the file was copied while in use or stored on a network share with unreliable connectivity. Compact and Repair is not a substitute for recovering a genuinely corrupted file; it's maintenance.
A practical pattern many Access teams use: keep the back-end data file on a stable file server, give each user their own copy of the front-end, and compact the front-end regularly. That reduces bloat and the chance of a shared-file lock conflict during maintenance.
SQL Server has no "Compact and Repair": what to run instead
In SQL Server, the phrase maps to four separate concerns, and mixing them up causes most of the trouble.
Fragmentation is handled by rebuilding or reorganizing indexes. Free space inside data files is reclaimed by shrink operations, which you should use sparingly. Free space at the database level is what you actually want back, and that's a file-size question, not a repair. Corruption is detected by DBCC CHECKDB and fixed by restoring from backup, with repair options as a last resort.
Here's the sequence I'd follow for a database that "feels slow and bloated," with checks at each stage.
1: Back up first, and verify the backup. A backup you haven't restored or checked isn't a safety net. 2: Run integrity checks. DBCC CHECKDB on the database confirms logical and physical consistency. Schedule it during a quiet window, because it's I/O-heavy. If it reports errors, stop normal maintenance and go to the corruption section below. 3: Measure fragmentation before rebuilding. Query sys.dm_db_index_physical_stats for the indexes that matter, and look at avg_fragmentation_in_percent alongside page count. Small indexes fragment trivially and aren't worth rebuilding. Common thresholds in Microsoft's guidance are roughly 5–30 percent for reorganize and above 30 percent for rebuild, but page count matters as much as the percentage; a 40-page index doesn't benefit. 4: Rebuild or reorganize selectively. ALTER INDEX ... REBUILD for the fragmented, sizable indexes; REORGANIZE when you need an online, lower-impact operation. Rebuilding everything on a schedule is a common habit that generates huge log growth for little gain. 5: Check free space inside files with a query against sys.dm_db_file_space_usage or the file size versus space-used columns. If a file is mostly empty because you deleted a large table, a shrink is defensible as a one-time cleanup, followed by a sensible autogrowth setting. Shrinking on a schedule is not. 6: Update statistics after large index operations, since rebuilds refresh statistics on the affected indexes but other statistics may be stale.
Success check: CHECKDB returns clean, the targeted indexes show acceptable fragmentation, and application query performance is stable or better. If performance got worse after a shrink, that's expected: shrinking fragments the file and can leave you paying for autogrowth later.
Shrinking SQL Server files without hurting performance
Shrinking deserves its own section because it's the step people most often get wrong when they translate "compact" from desktop software.
A shrink operation moves pages from the end of a file toward the front and truncates the tail. It doesn't reorganize data logically, and it can scatter pages across the file, raising fragmentation. It also leaves the file small, so the next burst of growth triggers autogrowth, which pauses activity and, if growth increments are small, repeats over and over.
The workable approach: treat shrink as a one-time correction after a genuinely large deletion, then set file growth to a reasonable fixed size (for example, hundreds of megabytes rather than a few megabytes) so growth events are rare. If you have multiple data files in a filegroup, shrinking can also unbalance them, and the fix is to empty a file with DBCC SHRINKFILE with the EMPTYFILE option before removing it. Note that shrinking a database below its original size is not possible; the floor is the size at creation.
There's also a compactness trade-off that doesn't involve shrink at all: data compression and columnstore indexes. If the goal is "make this database smaller and faster to scan," compression is often the better lever than file shrinking, because it reduces storage without the fragmentation tax.
When DBCC CHECKDB finds corruption
Corruption changes the job from maintenance to recovery, and the order of operations is strict.
1: Don't run repair options first. DBCC CHECKDB with REPAIR_ALLOW_DATA_LOSS can remove pages, and it should be a last resort after backups fail, not a first response. 2: Restore from a known-good backup into a separate database if possible, check it, then plan the cutover. 3: If you must repair, run DBCC CHECKDB with the least destructive repair option first (REPAIR_REBUILD), understand what each option does, and keep a copy of the damaged database before any repair. Page-level restore from a backup plus log can sometimes recover a specific damaged page without a full restore, which is preferable when it's available. 4: Investigate the cause afterward: failing storage, memory errors, or abrupt shutdowns are common. Repairing without fixing the cause just schedules the next incident.
Stop condition: if CHECKDB won't complete cleanly or reports errors you can't resolve with a restore, involve your database administrator or a data-recovery specialist before running further repair commands.
SQLite: VACUUM and integrity checking
SQLite has no "Compact and Repair" command, but it has the two pieces. VACUUM rebuilds the database file, reclaiming space from deleted rows and defragmented pages, and it can be run from the command-line shell or through the API.
1: Back up the file while no process is writing to it. If the app uses WAL mode, copy the database and any -wal and -shm files together, or checkpoint first. 2: Run PRAGMA integrity_check; a result of "ok" means the file structure is consistent. If it reports errors, stop and restore from backup before attempting VACUUM. 3: Run VACUUM. It needs free disk space roughly equal to the database size, because it builds a new file and swaps it in. It can't run inside a transaction, and it takes an exclusive lock while it works. 4: Consider auto_vacuum if you need incremental reclamation, but set it before the database grows; changing it later requires a VACUUM to take effect.
Success check: integrity_check returns "ok" and the file size drops after deleting a large amount of data. If VACUUM fails with a "database or disk is full" error, free space and retry rather than forcing it.
A quick comparison of the three engines
| Concern | Microsoft Access | SQL Server | SQLite |
|---|---|---|---|
| Built-in "compact and repair" command | Yes | No | No |
| Routine maintenance operation | Compact & Repair | Index rebuild/reorganize, CHECKDB | VACUUM, integrity_check |
| Space reclamation | Compact & Repair | Shrink (sparingly) | VACUUM |
| Corruption handling | Restore backup | CHECKDB, restore, repair as last resort | integrity_check, restore backup |
| Exclusive access needed | Yes | Maintenance windows for heavy operations | Yes, during VACUUM |
| Typical risk | Recompacting a damaged file | Over-shrinking and fragmentation | Running VACUUM with insufficient disk space |
Read the table as routing, not as a ranking: the right operation depends entirely on which engine you're running.
Prevention beats repair in every engine
The reason compact-and-repair questions come up at all is that databases accumulate waste. A few habits cut the frequency.
In Access, split front-end and back-end, compact the front-end on a schedule, and avoid storing large attachments in the file. In SQL Server, set sensible autogrowth, schedule CHECKDB, rebuild only what fragmentation data justifies, and keep backups tested. In SQLite, delete in sensible batches, run VACUUM during quiet periods or enable incremental vacuum if you need it, and check integrity after any unexpected shutdown.
None of these replace a backup. Every operation in this guide assumes you have one, and the single most common failure is skipping that step because the maintenance feels routine.
FAQ
Can I compact and repair a database while other people are using it?
Not in Access; you need exclusive access. SQL Server index operations can run online in some editions, but CHECKDB and heavy maintenance normally belong in a maintenance window. SQLite's VACUUM requires an exclusive lock for the duration.
Does compacting an Access database fix corruption?
Sometimes it clears minor structural issues, but it isn't a recovery tool. If Access reports the file as damaged or won't open it, restore the backup rather than running Compact and Repair repeatedly.
Is shrinking a SQL Server database the same as compacting it?
No. Shrinking reclaims file space but fragments data, and it doesn't rebuild indexes or fix corruption. Rebuilding fragmented indexes is closer to "compacting"; integrity checking is closer to "repairing."
How often should I run these operations?
Access: regularly, especially on frequently used front-ends. SQL Server: CHECKDB and targeted index maintenance on a schedule driven by your data's change rate; shrink only after large deletions. SQLite: VACUUM when the file has grown noticeably from deletions, after an integrity check passes.