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:
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:
Updated: You also can do the same thing programmatically by putting those lines into your
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
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:
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!).