Free space in your WordPress install by deleting old image sizes

If you change your theme often, your uploads folder will accumulate thumbnails of images in many sizes that you no longer use. This consumes disk space unnecessarily. I wish someone coded a plugin for this, but failing that, a handy way to do this via SSH is:

find . -name *-250x250.* | xargs rm -f

Where 250×250 is the image size you want to delete. You could also try something like:

find . -name *-250x*.* | xargs rm -f

if you have multiple thumbnail sizes like 250×250 250×300 etc.

What I do is list images in the folders to see the unwanted sizes there, and run this delete a few times with various sizes. A more ruthless person could try something like:

find . -name *-*x*.* | xargs rm -f

I do not recommend this, as it can match several files that you may not want to delete, for example a file with a hyphenated name and the letter x in the last hyphenated word, like “wish-merry-xmas.jpg” for example, which wouldn’t be a resized image, but an original or worse, it could be another file and not an image at all, like “here-are-exact-directions-to-our-property.html”.

But if you have a lot of thumbnail sizes, you may feel tempted anyway. Two suggested precautions. Change directory to your uploads folder (you should do this in any case)
cd /path/to/wprdpress/wp-content/uploads
find . -name *-*x*.* | xargs rm -f

The other precaution to take is to specify extension.
find . -name *-*x*.jpg | xargs rm -f
find . -name *-*x*.png | xargs rm -f

This will give you some protection from inadvertently deleting non-resize uploads like “entertaining-extras.pdf”

of course, if you are a patient soul (or don’t have too many files uploaded), you could find the files before deleting to see if any other files are getting selected along with resizes.

find . -name *-*x*.*
and if all is well
find . -name *-*x*.* | xargs rm -f

Do you have a better method?

Enhanced by Zemanta

Comments 2

  • […] Check to see available space on your disk. If your disk is full you should try upgrading your hosting package or freeing up some space on the disk. One easy way might be to delete old image thumbnails in sizes you no longer use. […]

  • Hey, instead of using xargs you can use the more safe -exec param like this:

    find . -name *-*x*.* -exec rm {} \;

    or with echo to see what it will do, or you can use \+ instead to maximize the number of arguments sent to rm, like this:

    find . -name *-*x*.* -exec rm \+

    It is safer as if a file will begin with a minus it could be not safe to send it to another command with xargs, as far as I know it can be treated as an option, also it is safer for names with spaces, tabs and other weird and funny characters in the name.

    Passing unsafe name can be a security vulnerability as if there is a filename like “bar -rf index.php” then “-rf” will be treated as an option.
    You can try this unfortunate behaviour in an empty directory:

    “touch — bar\ ref\ index.php; find . | xargs echo”

    You’ll get: “echo: invalid option — ‘rf'”, now imagine passing this to rm! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *