When we change our WordPress themes, we often forget that default image sizes have changed too. As most plugins to resize WordPress images don’t remove the current resized images, our wp-content/uploads or wp-content/blog.dir directories become full of abandoned images resulting in wasted disk space.

The command lines below will remove current resized images, including Retina versions so that we can correctly resize WordPress images per the new theme dimensions. And yes, we’re using the command line, because I (Michael) am old school.

SHOW all resized images

cd /home/example/public_html/wp-content/uploads
find . -regextype posix-extended -regex ".*-[[:digit:]]{2,4}x[[:digit:]]{2,4}(@2x)?.(jpg|jpeg|png|eps|gif)" -type f
cd /home/example/public_html/wp-content/blogs.dir
find . -regextype posix-extended -regex ".*-[[:digit:]]{2,4}x[[:digit:]]{2,4}(@2x)?.(jpg|jpeg|png|eps|gif)" -type f

DELETE all resized images

cd /home/example/public_html/wp-content/uploads
find . -regextype posix-extended -regex ".*-[[:digit:]]{2,4}x[[:digit:]]{2,4}(@2x)?.(jpg|jpeg|png|eps|gif)" -type f -exec rm {}  \;
cd /home/example/public_html/wp-content/blogs.dir
find . -regextype posix-extended -regex ".*-[[:digit:]]{2,4}x[[:digit:]]{2,4}(@2x)?.(jpg|jpeg|png|eps|gif)" -type f -exec rm {} \;

Rebuild to Resize WordPress Images

After deleting the resized media images, we can regenerate the sizes needed by using the Regenerate Thumbnails or AJAX Thumbnail Rebuild WordPress plugins.

Stuck? Axelerant's WordPress Support is What You Need Learn More

This article was originally written in September 2011 and has been updated since for clarity.

  • Måns Jonasson

    Hey,

    I ran your commands, but discovered a small but very important error.

    The delete-lines are missing the actual deleting command, so the script will attempt to execute each file instead of deleting them.

    Thus, the lines SHOULD read:

    cd /home/example/public_html/wp-content/uploads
    find . -regextype posix-extended -regex “.*-[[:digit:]]{3}x[[:digit:]]{3}.(jpg|png)” -type f -exec rm {} \;
    cd /home/example/public_html/wp-content/blogs.dir
    find . -regextype posix-extended -regex “.*-[[:digit:]]{3}x[[:digit:]]{3}.(jpg|png)” -type f -exec rm {} \;

  • okay this seems like a silyy question perhaps.. but where do you write these command codes? ive been searching for hours how to to get rid of these duplicate images, and this seems like the only viable option but i have no idea where to put this code ! :0

    • Jonathon, You need to have shell access to your website. Otherwise, this technique isn’t feasible.

      If you don’t know what SSH is or what these commands are doing or why, I strongly suggest ensuring you have a current file and database backup before attempting these commands.

      If you’d like this work to be done for you, please purchase a single issue request via http://aihr.us/downloads/website-support-agreement/.

  • Chris

    Hey do you by any chance know how to do this with the OSX terminal?

  • Katka

    Hi,
    my friend runs website on windows server.

    The following commands are recognized:
    OPTS CLNT CSID NOOP HELP FEAT HOST USER PASS REIN QUIT SITE ABOR SYST STAT AUTH PBSZ PROT CCC SSCN CWD XCWD CDUP XCUP PWD XPWD MKD XMKD RMD XRMD RMDA DSIZ AVBL PORT EPRT PASV EPSV TYPE STRU MODE RETR STOR APPE STOU THMB ALLO REST SIZE RNFR RNTO DELE XDEL MDTM MFMT MFCT MFF XCRC XMD5 XSHA1 XSHA256 XSHA512 COMB LIST NLST MLST MLSD

    Is it possible to do it with some of these commands?

    Many thanks

    • Katka,

      I’m sorry, I don’t think it’s possible. Please don’t consider it as a final word, though.

      The problem I see is that there’s a lack of a `find` equivalent command that supports the regular expressions being used.

      If you happen to have a descriptive list of these server commands, I might see something to get you closer.

      Michael

  • Jonathan Butterick

    On OSX, this is what works for me: find -E . -type f -iregex ‘.*[0-9]{2,3}x[0-9]{2,3}.*.(jpg|jpeg|png|eps|gif)’ -exec rm {} ;

    • Thank you Jonathan for the tip. I just realized that I have XCode command line tools and GNU utilities installed via Brew which gives me the regex option which might cause a headache for others.

      `michael@mbma:~ $ find –version
      find (GNU findutils) 4.4.2`

  • Jonathan Butterick

    And while we’re at it, this selects all the *non* resized images (and then ZIPs them up for good measure)

    find . -type f | egrep -vi ‘.*[0-9]{2,3}x[0-9]{2,3}.*.(jpg|jpeg|png|eps|gif)’ | zip images.zip -@

    • Great idea Jonathan. Do keep them coming. Do you have a blog?

      • Jonathan Butterick

        Nope! I actually rarely post anywhere, but this topic piqued my interest.

        • If this piqued your interest, you definitely must have had this problem at one time. Thank you for the multiple contributions.

  • If you’re running WP Retina 2x, you can run a second command to also delete all @2x variations:

    find . -regextype posix-extended -regex “.*-[[:digit:]]{3}x[[:digit:]]{3}@2x.(jpg|png|jpeg)” -type f -exec rm {} ;

    I’m sure someone with a bigger talent for regex than myself could adjust this so we can delete both non-2x and 2x versions in a single command! :)
    Notice I’ve also adjusted it to include .jpeg files, something that could be done in Michael’s original commands as well, should you need it. I also had to run it a couple more times adjusting the regex from {3}x{3} digits to {4}x{4} followed by {4}x{3} digits to accommodate for my larger image sizes. Again, calling regex pros to adjust the command to allow all combinations at once! :)

    Thanks Michael!

    • Marcus, I’m very sorry about the extended delay in updating. I’ve had a chance to account for Retina and image sizes of 2- to 4-digits in the regular expression. My local testing seems to work, can you please try as well? Thank you, Michael