Remove Redundant Image Sizes And Files In WordPress

Ajax Rebuild Thumbnails

Each WordPress theme has a bundle of image sizes, and everytime we switch theme, we’ll use other image sizes. That will lead us to a problem of redundant image files left in WordPress folder! They occupy a large space of our host, and make us hard to do a backup because of unacceptable running time and file size. This post will help you solve this problem.

Firstly, to remove registered image sizes, you have to delete the code in your WordPress theme/plugin that registers image sizes, like this:

add_image_size( 'my-thumb', 100, 100, true );

Maybe you not have to do this because all registered image sizes are needed. Just ignore this step in that case.

Secondly, you might want to remove built-in thumbnail sizes: thumbnail, medium, large. They’re registered by WordPress by default and you can’t remove them with the technique above. There’s a trick in this case: put zero 0 in thumbnail sizes (width and height) will disable these sizes, like this:

Remove built-in thumbnail sizes

Updated: You also can do the same thing programmatically by putting those lines into your functions.php file:

update_option( 'thumbnail_size_h', 0 );
update_option( 'thumbnail_size_w', 0 );
update_option( 'medium_size_h', 0 );
update_option( 'medium_size_w', 0 );
update_option( 'large_size_h', 0 );
update_option( 'large_size_w', 0 );

Thirdly, we’re going to remove redundant files generated for those sizes.

If you want to remove files of specific size (for ex. 180x180), run this command via SSH:

find . -name *-180x180.* | xargs rm -f

This will remove all files with name *-180x180.* (such as screenshot-180x180.jpg), which are generated by WordPress.

Note: You can only do this if you have privilege of running Linux command on your host/server. If you don’t (in case of using shared host), you should ask your host support to do this for you.

If you want to remove all generated files, run this command:

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

Warning: It will remove all files with name *-*x*.* (image1-80x80.png, image2-120x100.jpg). If this pattern matches one of your original file names, then you’ll loose it. So be careful and make sure you know what you’re doing!

After remove all redundant files, you should regenerate thumbnails. The very popular plugin Regenerate Thumbnails can do that for you, but it generates all thumbnails for images, which is might not what you want. Another plugin AJAX Thumbnail Rebuild can help you rebuild thumbnails for only selected sizes:

AJAX Thumbnail Rebuild

After doing all of these techniques, your WordPress folder is much more cleaner now. That is the good time to do a backup (you’ll realize the size of backup file is dramatically decreased!).

21 thoughts on “Remove Redundant Image Sizes And Files In WordPress

  1. Thanks Rilwis, for sharing this useful technique.

    My question is can we remove(or set the size to 0) for built in image sizes (thumbnail, medium, large) in theme code.

    As, I think this way we can control the redundant images from the start. (I am asking this in context that mostly my theme buyers install the fresh WordPress and deploy the theme on their sites. So, if I can provide it in my theme code then they can avoid having redundant images from start.)

    Regards

  2. How can we d o for images with various sizes ? Example : wifht 500px; height auto (or max height 9999, same problem).
    A plugin for removing images will be really helpful…

    1. You can modify the pattern to delete images with 500px width:

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

      And yes, I’d love to have a plugin to do all these things for us. But unfortunately there’s not such one.

  3. Great Trick to clean up when you Regenerated Thumbnails, especially when doing it over and over again while developing.

    The code above gave me an error of “unkown primary or operator”, but worked when i added some double-quotes to the path like that:

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

    Thanks!

  4. [email protected] [/home/windowsf/public_html/wp-content/uploads]# find . -name *-*x*.* | xargs rm -f
    find: paths must precede expression: windows-xp_thumb.gif
    Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path…] [expression]

    Please advise, the delete command is not working.

    1. Hi Cosmin, I had to put the filename part of the command in quotes, e.g.

      find . -name “*-277×277.*” | xargs rm -f

  5. Anh Tuấn Anh có thể làm 1 site với nội dung tiếng việt đc không ạ. Blog anh nhiều bài hay, nhưng tiếng anh ko tốt nên có nhiều đoạn không hiểu 🙂
    Follow bài viết trên site anh bằng cách nào ạ

    1. Chào Thái, anh mới bắt đầu chuyển sang viết tiếng Việt ở đây: https://deluxeblogtips.com/vi/. Các bài cũ thì chịu khó đọc tạm tiếng Anh nhé, các bài mới giờ sẽ có song ngữ 🙂

      Anh mới thêm lại các nút follow trên blog (góc trên bên phải), hồi xưa bỏ sạch cho nó nhẹ :D. Em follow nhé!

  6. Hello
    I’ve try all code above but not working, please anybody tell me the code is correct
    So my skill english is not well hope everyone cound understand!
    Many thanks,

  7. Yikes, I don’t recommend running find . -name *-*x*.* | xargs rm -f on your WordPress account. It removes necessary WordPress core files, plugin files and more. Thankfully I keep backups. LOL.

    I do recommend running find . -name *-180×180.* | xargs rm -f on any file sizes you know you aren’t using, such as switching themes that use different sizes. That was awesome. Cleaned up 300MB of crap.

    1. You need to run in shell (bash, command line). Try to connect to your host using ssh and run the command. If your host doesn’t have ssh access, then you can’t do this.

    2. You can also run it on Cyberduck if you’re using Mac. But you need to make sure you’re connecting via SSH. You also need to find out what port your hosting provider uses for SSH. In our case, since we are using GoDaddy (not for long, thankfully!) we had to connect via port 22.

  8. Great article! Thank you for sharing.

    I had a major issue when I sent our server the command. It deleted not only all the images but vital PHP files, not sure why! I was able to recreate all missing files and got it back up fast. The solution was really useful to get rid of unnecessary space, especially now that I’m getting ready to move the site.

    For whatever reason, no Regenerate plugin was able to regenerate our thumbnails, so I went in to the template files and manually updated the thumbnail sizes being called and it brought up the original photo sized according to my specs.

    Well, thanks again. I thought I’d share that with you.

Comments are closed.

Newsletter

Subscribe to our newsletter to receive news and tutorials about WordPress and web development.

Memory - An elegant, clean and powerful WordPress blog theme! Learn more →