Optimizing WordPress: Removal of Unnecessary Image Sizes

WordPress generates multiple image sizes by default, but not all of them are essential for your site. By selectively removing certain image sizes, you can reduce storage usage and improve loading times for your pages.

To achieve this, we will need the intermediate_image_sizes_advanced filter.

Identify Unused Image Sizes:

Before proceeding, identify which image sizes are unnecessary for your site. Common sizes include thumbnail, medium, large, and full-size images.

Remove Specific Image Sizes via functions.php:

To remove unwanted image sizes, add the following code snippet in your theme’s functions.php file:

function remove_image_size($sizes) {
            unset($sizes['thumbnail']);
            unset($sizes['medium']);
            unset($sizes['medium_large']);
            return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'remove_image_size');

In this example, we remove the thumbnail, medium, and medium_large image sizes.

Removing Specific Image Sizes:

function remove_image_sizes() {
    foreach ( get_intermediate_image_sizes() as $size ) {
        if ( in_array( $size, array( '1536x1536','2048x2048' ) ) ) {
            remove_image_size( $size );
        }
    }
}
add_action('init', 'remove_image_sizes');

Removing All Image Sizes Except (‘thumbnail’, ‘medium’, ‘medium_large’, ‘large’):

function remove_image_sizes() {
    foreach ( get_intermediate_image_sizes() as $size ) {
        if ( !in_array( $size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
            remove_image_size( $size );
        }
    }
}
add_action('init', 'remove_image_sizes');

Regenerate Thumbnails

After removing image sizes, regenerate thumbnails using a plugin like “Regenerate Thumbnails” to update existing images with the new size settings.

By selectively removing unnecessary image sizes in WordPress, you can optimize your site’s media library and reduce server storage space. This practice not only enhances your site’s performance but also contributes to a more efficient and streamlined content management system.