How to remove generated image sizes in WordPress with CLI

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 backup because of unacceptable running time and file size. This post will help you solve this problem.

Remove image sizes

Remove registered image sizes in your theme

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.

Remove built-in WordPress image sizes

Secondly, you might want to remove built-in WordPress image 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

You also can do the same thing programatically 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( 'medium_large_size_w', 0 );
update_option( 'medium_large_size_h', 0 );
update_option( 'large_size_h', 0 );
update_option( 'large_size_w', 0 );

However, there are other image sizes that are hidden:

  • post-thumbnail: the image size used for post thumbnails. It's usually registered in your theme, but the name of the image size is always fixed to post-thumbnail. It's used in a lot of places in WordPress.
  • medium_large: an auto-generated image size by WordPress which has default width of 768 px.
  • 1536x1536 and 2048x2048: are big image sizes that WordPress auto generates for high-density devices.

Since version 5.3, WordPress also auto generates 2 additional image sizes:

  • Scaled down version of a large image: when a large image is uploaded, it is stored in the uploads directory but is not used on the web site. A new scaled down image is created and used as the largest available size. This scaled down version has the name of original-name-scaled.jpg.
  • Rotated version of a large image: if an image is uploaded and has exif data, it might be rotate to the correct orientation. This rotated version has the name of original-name-rotated.jpg.

These image sizes are generated on-the-fly, so there are no options for them. However, you can disable this behavior using the Falcon plugin:

Disable big image sizes with Falcon

Remove image files

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. I'd suggest using the reGenerate Thumbnails Advanced plugin, which lets you rebuild thumbnails for only selected sizes:

reGenerate Thumbnails Advanced

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

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Leave a Reply

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


Copyright © 2024 Deluxe Blog Tips. All rights reserved.

My products: Meta Box | Slim SEO