Disk images on a Mac: .dmg, .sparsebundle and .sparseimage leftovers and hdiutil compact

By the Zofle team · 22 November 2026 · 6 min read

Disk image bands and compaction on macOS

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:

FormatStructure on Host SSDDynamic Growth?Backup / Sync Efficiencyhdiutil compact Support?
.dmg (UDIF)Single monolithic fileNo (fixed allocation or compressed)Poor: any edit rewrites whole fileNo (fails: Function not implemented)
.sparseimageSingle monolithic fileYes (allocates blocks on demand)Poor: file changes continuously as data growsYes (truncates free payload blocks)
.sparsebundleDirectory containing 8 MiB band filesYes (allocates new 8 MiB bands on demand)Excellent: backup tools only copy modified bandsYes (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.

bash
# 1. Create a 500 MB APFS sparse bundle
hdiutil create -size 500m -type SPARSEBUNDLE -fs APFS -volname SBTest sb_test.sparsebundle

# 2. Attach and write 120 MiB of random data
hdiutil attach sb_test.sparsebundle
dd if=/dev/urandom of=/Volumes/SBTest/fill.bin bs=1m count=120
sync
hdiutil detach /Volumes/SBTest

# 3. Re-attach, delete the file, and detach
hdiutil attach sb_test.sparsebundle
rm /Volumes/SBTest/fill.bin
sync
hdiutil detach /Volumes/SBTest

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:

StagePhysical Size on Host SSD (du -sk)Band Files in bands/Apparent Volume SpaceHost Space Reclaimed
Freshly created (empty)8,252 KB (~8.1 MB)3 bands500 MB capacity (498 MB free)N/A
Written 120 MiB payload132,660 KB (~129.5 MB)18 bands378 MB free0 KB
File deleted inside volume132,660 KB (~129.5 MB)18 bands498 MB free (internally)0 KB (Zero reclaimed!)
After hdiutil compact8,816 KB (~8.6 MB)3 bands500 MB capacity120.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:

bash
hdiutil compact sb_test.sparsebundle

# Output:
# Starting to compact…
# Reclaiming free space…
# Finishing compaction…
# Reclaimed 120.2 MB out of 498.2 MB possible.

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:

bash
# Sparse Image test:
hdiutil compact si_test.sparseimage
# Output: Reclaimed 123 MB out of 498.2 MB possible.
# Physical size dropped from 130,052 KB down to 4,100 KB.

# Standard DMG test:
hdiutil compact dmg_test.dmg
# Output: hdiutil: compact failed - Function not implemented

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.

bash
# 1. Find all disk images over 200 MB anywhere on indexed volumes
mdfind '(kMDItemFSName == "*.dmg" || kMDItemFSName == "*.sparsebundle" || kMDItemFSName == "*.sparseimage") && kMDItemLogicalSize > 209715200'

# 2. Or query directly by Apple Uniform Type Identifier (UTI)
mdfind 'kMDItemContentType == "com.apple.disk-image-udif" || kMDItemContentType == "com.apple.disk-image-sparse-bundle" || kMDItemContentType == "com.apple.disk-image-sparse"'

# 3. Fast check for images in your user home folder without Spotlight
find ~ -maxdepth 4 \( -name "*.dmg" -o -name "*.sparsebundle" -o -name "*.sparseimage" \) -size +200M 2>/dev/null

Safe cleanup rules before you delete or compact

Before running compaction or moving disk images to Trash, follow three safety checks:

Image TypeLocationAction to Take
Installer .dmg files~/DownloadsIf the application is already dragged into /Applications, move the DMG to Trash. It is static dead weight.
Time Machine .sparsebundleNAS or external backup drivesNever 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 foldersAlways 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 Mac

Sources

  • 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.

One email when it's ready. No spam, ever.