Disk images on a Mac: .dmg, .sparsebundle and .sparseimage leftovers and hdiutil compact
When you mount a disk image on a Mac, copy gigabytes into it, and later delete those files, you naturally expect your host Mac SSD to gain that space back. It does not. Not a single byte is freed automatically. The container file or bundle on your drive stays at its maximum historical peak, quietly holding onto storage you thought you had wiped.
Disk images act as virtual drives. Inside the image, the guest filesystem (APFS or HFS+) maintains its own internal allocation bitmap. When you run rm inside the mounted disk image, the guest filesystem marks those data blocks as free for future writes inside the image. But the container wrapping those blocks on your host APFS drive does not shrink itself. To see exactly where the space goes and how to recover it, we ran an empirical test on Apple Silicon with .sparsebundle, .sparseimage and plain .dmg formats.
The three disk image formats on macOS
macOS supports three main disk image architectures, each designed for different workflows and storage constraints:
| Format | Structure on Host SSD | Dynamic Growth? | Backup / Sync Efficiency | hdiutil compact Support? |
|---|---|---|---|---|
.dmg (UDIF) | Single monolithic file | No (fixed allocation or compressed) | Poor: any edit rewrites whole file | No (fails: Function not implemented) |
.sparseimage | Single monolithic file | Yes (allocates blocks on demand) | Poor: file changes continuously as data grows | Yes (truncates free payload blocks) |
.sparsebundle | Directory containing 8 MiB band files | Yes (allocates new 8 MiB bands on demand) | Excellent: backup tools only copy modified bands | Yes (deletes band files containing freed blocks) |
Apple introduced .sparsebundle in Mac OS X Leopard specifically for Time Machine network backups. In a monolithic 100 GB .sparseimage, appending 5 MB of data modifies file headers and scattered blocks, forcing backup utilities like Time Machine, rsync, or cloud drives to re-read and re-upload the entire 100 GB file. A .sparsebundle solves this by splitting the disk payload into discrete 8 MiB files inside a bands/ directory. Only the modified 8 MiB bands need to be transferred.
The experiment: filling and deleting 120 MiB on a sparse bundle
We created a fresh 500 MB APFS sparse bundle (sb_test.sparsebundle), filled it with a 120 MiB binary payload, measured the physical band files created on the host drive, deleted the file inside the volume, and observed host SSD usage before and after.
Here are the exact measurements taken directly from du -sk and the band file inventory in sb_test.sparsebundle/bands across each stage of the experiment:
| Stage | Physical Size on Host SSD (du -sk) | Band Files in bands/ | Apparent Volume Space | Host Space Reclaimed |
|---|---|---|---|---|
| Freshly created (empty) | 8,252 KB (~8.1 MB) | 3 bands | 500 MB capacity (498 MB free) | N/A |
| Written 120 MiB payload | 132,660 KB (~129.5 MB) | 18 bands | 378 MB free | 0 KB |
| File deleted inside volume | 132,660 KB (~129.5 MB) | 18 bands | 498 MB free (internally) | 0 KB (Zero reclaimed!) |
After hdiutil compact | 8,816 KB (~8.6 MB) | 3 bands | 500 MB capacity | 120.2 MB reclaimed! |
Notice the crucial moment at Stage 3: inside /Volumes/SBTest, df -h reported that 498 MB was completely free. But on the host Mac SSD, sb_test.sparsebundle remained at 132.6 MB and still held all 18 band files. The host filesystem has no idea that the 15 data bands contain stale or unreferenced data until an explicit compaction step inspects the container.
Reclaiming the dead space with hdiutil compact
To reclaim the storage, the disk image must first be detached (unmounted). Then run hdiutil compact against the bundle path:
During compaction, macOS scans the internal APFS allocation tables of the disk image, identifies all 8 MiB bands that contain only unallocated or zeroed blocks, and permanently deletes those band files from the host filesystem. In our test, 15 band files were instantly pruned, dropping host usage from 132.6 MB down to 8.8 MB.
Comparing .sparseimage and standard .dmg
We repeated the exact same 120 MiB write-and-delete experiment with a single-file .sparseimage and a standard .dmg:
Compaction works effectively on .sparseimage because APFS supports sparse file hole punching and payload truncation. However, on standard UDIF .dmg disk images, hdiutil compact fails with Function not implemented. Standard DMGs cannot be compacted; they can only be deleted, or converted to a sparse format using hdiutil convert -format UDSB.
Finding old, bloated disk images with mdfind
Disk images often hide in ~/Downloads, ~/Library/Developer, virtual machine folders, or external backup partitions. Crawling your entire SSD with find / takes minutes and causes unnecessary drive thrashing. Because macOS indexes filesystem metadata with Spotlight, you can query Spotlight's database directly with mdfind in 0.05 seconds.
Safe cleanup rules before you delete or compact
Before running compaction or moving disk images to Trash, follow three safety checks:
| Image Type | Location | Action to Take |
|---|---|---|
Installer .dmg files | ~/Downloads | If the application is already dragged into /Applications, move the DMG to Trash. It is static dead weight. |
Time Machine .sparsebundle | NAS or external backup drives | Never delete while a backup is in progress. Detach first, then run hdiutil compact /path/to/backup.sparsebundle to recover freed space from pruned snapshots. |
| Active virtual machine disks | ~/Virtual Machines or UTM folders | Always shut down the guest VM completely before compacting. Compacting an active, attached image can corrupt guest data. |
To verify whether an image is currently attached before running compaction, run hdiutil info. If the image path appears under active mount points, detach it with hdiutil detach /Volumes/<VolumeName> first.
Zofle automatically surfaces removable installer DMGs and detached disk images so you can reclaim gigabytes without guessing.
Download Zofle for MacSources
- Apple Inc.,
hdiutil(1)macOS Developer and Darwin Manual Pages. - Apple Support, Create a disk image using Disk Utility on Mac.
- Apple Open Source, APFS and disk image sparse bundle architecture documentation. Checked 22 November 2026.
Zofle Pro news
People signing up now will get Zofle Pro, with more features, for free when we launch it.