Sparse bundles grow to fit and do not shrink when you delete
A disk image keeps the space it once used, even after you empty it. How to compact one, and how to find images you mounted once and forgot.
Disk images turn up on a Mac in three ways: as the installers you downloaded, as encrypted containers people make for private files, and as the format Time Machine uses over a network. All three have the same behaviour, which is that they grow to fit what you put in and do not shrink when you take it out.
Find every image on the Mac
find ~ -type f \( -name '*.dmg' -o -name '*.sparseimage' \
-o -name '*.sparsebundle' -o -name '*.iso' \) -size +100M \
-exec du -sh {} + 2>/dev/null | sort -hOn most Macs the answer is a folder of installers in Downloads, which are safe to remove once the app is installed. The interesting results are the sparse images, because those are ones you created.
Why an emptied image still uses the space
A sparse image allocates blocks as data is written and keeps them allocated afterwards. Deleting a file inside marks the space free within the image, but the image file on your disk is still the size it grew to. This is the same behaviour as a virtual machine disk, for the same reason.
hdiutil compact ~/Documents/Archive.sparsebundleCompacting returns the unused blocks. The image must not be mounted when you run it, and the operation can take a while on a large image. It is safe: nothing inside the image is touched, only the empty space around it.
Encrypted images people forget
An encrypted sparse image made years ago for a few sensitive documents can be tens of gigabytes if it was ever used as a working area. It looks like one file and cannot be inspected without the password, which is exactly why it survives every cleanup.
Mount it, check what is inside, copy out what you need, and either compact it or remove it entirely. An image you cannot unlock is a special case: without the password there is nothing to be done but delete it, so it is worth being certain before you do.
Network Time Machine backups
A Time Machine backup to a network drive is stored as a sparse bundle, and it grows in the same way. Do not compact or edit those by hand. Manage them through Time Machine, which is covered in how Time Machine uses disk space.
Common questions
Why does my sparse bundle not shrink when I delete files?
Because it keeps the blocks it once allocated. Deleting inside the image frees space within it but leaves the file on disk at the size it grew to. Running hdiutil compact on the unmounted image returns the unused blocks.
How do I compact a disk image on a Mac?
Unmount it, then run hdiutil compact followed by the path to the image. It is safe for the contents and can take a while on a large image, but it is the only way to return the space a sparse image has grown into.
Can I delete .dmg files after installing an app?
Yes. A disk image is only the container the app arrived in. Once the application is in your Applications folder the image has no purpose, and it can be downloaded again if ever needed.
Should I compact a Time Machine sparse bundle?
No. Network Time Machine backups are stored as sparse bundles, and they should be managed through Time Machine rather than with disk image commands. Editing one by hand risks the whole backup.