Shrink a .sparseimage

14

5

I have a .sparseimage that occupies 90GB of space on my hard drive despite only having 30GB of files in it. How can I reclaim the empty space used by the .sparseimage and compact the file?

squircle

Posted 2011-04-25T14:23:15.200

Reputation: 6 365

Answers

15

After some cursory searching, I found a forum post pointing to hdiutil's compact verb. From the man page:

compact image

scans the bands of a sparse (SPARSE or SPARSEBUNDLE) disk image containing an HFS filesystem, removing those parts of the image which are no longer being used by the filesystem. Depending on the location of files in the hosted filesystem, compact may or may not shrink the image. For SPARSEBUNDLE images, completely unused band files are simply removed.

I ran hdiutil compact drive.sparseimage and it successfully reclaimed almost 98% of the space.

(I guess it's one of those days... I should really learn to Google stuff first).

Edit: I tested compact on a 1GB sparseimage with just a few text files, and it ran quite quickly, but my Mac is taking quite a while to compact the 90GB image. Be prepared to wait.

squircle

Posted 2011-04-25T14:23:15.200

Reputation: 6 365

3

If you get an error "hdiutil: compact failed – Function not implemented": http://apple.stackexchange.com/questions/132147/hdiutil-compact-failed-function-not-implemented

– traindriver – 2015-02-27T12:07:28.810

1Prepare for hours of fun watching that second progress indicator creep to the right edge of the Terminal. – Daniel Beck – 2011-04-25T14:35:48.097

@Daniel I edited my answer a bit. I tested it on a smaller sparseimage and it ran quite quickly, but it's taking quite a while on the large image. It's times like these I'm thankful I upgraded to a 7200RPM hard drive! – squircle – 2011-04-25T14:41:00.713

It's related to how much reorganizing that task needs to do. It's usually pretty fast if you dump some files into the sparseimage, pull them back out, and them compact, since the system can just throw away those bytes. – Daniel Beck – 2011-04-25T14:43:30.290

6

  1. Double click the .sparseimage file to open it
  2. Empty the trash
  3. Eject the .sparseimage drive
  4. Enter this into Terminal: hdiutil compact path-to-file.sparseimage

Hải Phong

Posted 2011-04-25T14:23:15.200

Reputation: 311

I like the addition of "Empty the trash" I think that is a step that is easy to overlook. – Eric – 2017-09-02T02:10:09.693