Disabling the creation of copies of images in WordPress

By default, WordPress generates four sizes for each original image we upload and another 2 copies when a huge image is uploaded. Also, themes and plugins can create additional image sizes.

As a result, your site can create 5 to 10 copies (duplicates) of each uploaded image. This can increase the required disk space and the size of backup files. It's good when all these copies are needed and used in the theme, but in fact this almost never happens! Therefore, it would be good to unregister the additional thumbnail sizes. Below, we'll discuss how to do this.

When should I remove image copy generation?

In all cases when the theme does not use all created sizes. Therefore, first, check if there are copies of images that are not used in the theme. Read below how to do this.

Numerous duplicates of uploaded images, which are placed in the uploads folder, can significantly increase the size of backup files - this is at least inconvenient. Imagine, you upload one image, and WordPress creates 7 more thumbnails for it - 7 copies of this image, while some of the duplicates can be almost the same size as the original. Out of these 7 copies, only one will go to the main page and even that will hang there until it's replaced by the next one. The used file will remain on the host for life, and it was used for a day or a week.

If you have thousands of images on your site, it can cause problems and difficulties.

How to find out which sizes are used on the site?

By default, WordPress creates the following copies:

  1. thumbnail — thumbnail (changeable from the admin panel).
  2. medium — medium size (changeable from the admin panel).
  3. medium_large — moderately large (since WP 4.4).
  4. large — large (changeable from the admin panel).
  5. 1536x1536 — 2x medium_large (since WP 5.3).
  6. 2048x2048 — 2x large (since WP 5.3).
  7. -scaled — see this note.

In addition to these sizes, additional sizes can be created by the theme itself. For example, an additional size for setting a post thumbnail, for posts in the sidebar, or for other purposes.

First of all, you need to determine how many copies are created when uploading an image. To do this, go to the admin panel and upload a large image, let it be an image of 1900x1000 pixels (a large size is needed so that all possible copies are created). Then go to the image upload folder, usually this is: /wp-content/uploads/. There, the files are sorted by years and months, go to the last year and month, there you should see the file that you just uploaded and all its copies:

My test blog, where there is not a single additionally registered theme size, created 7 copies

How to find out all registered thumbnail sizes

The example with image upload shows how many are created, but does not show what each size is called. The name (ID) of the size needs to be known to disable it.

There are several ways to find out what sizes are registered on the site.

Method 1: WP-CLI command

wp media image-size

$ wp media image-size
+---------------------------+-------+--------+-------+-------+
| name                      | width | height | crop  | ratio |
+---------------------------+-------+--------+-------+-------+
| full                      |       |        | N/A   | N/A   |
| twentyfourteen-full-width | 1038  | 576    | hard  | 173:96|
| large                     | 1024  | 1024   | soft  | N/A   |
| medium_large              | 768   | 0      | soft  | N/A   |
| medium                    | 300   | 300    | soft  | N/A   |
| thumbnail                 | 150   | 150    | hard  | 1:1   |
+---------------------------+-------+--------+-------+-------+

Method 2: PHP code

Add the following code to the theme file header.php or footer.php. This way, you will see what sizes exist on the site and what they are called.

/**
 * Get information about all registered image sizes.
 *
 * @global $_wp_additional_image_sizes
 * @uses   get_intermediate_image_sizes()
 *
 * @param  boolean [$unset_disabled = true] Remove from list sizes with 0 width and height?
 * @return array Data of all sizes.
 */
function get_image_sizes( $unset_disabled = true ) {
	$wais = & $GLOBALS['_wp_additional_image_sizes'];

	$sizes = array();

	foreach ( get_intermediate_image_sizes() as $_size ) {
		if ( in_array( $_size, array('thumbnail', 'medium', 'medium_large', 'large') ) ) {
			$sizes[ $_size ] = array(
				'width'  => get_option( "{$_size}_size_w" ),
				'height' => get_option( "{$_size}_size_h" ),
				'crop'   => (bool) get_option( "{$_size}_crop" ),
			);
		}
		elseif ( isset( $wais[$_size] ) ) {
			$sizes[ $_size ] = array(
				'width'  => $wais[ $_size ]['width'],
				'height' => $wais[ $_size ]['height'],
				'crop'   => $wais[ $_size ]['crop'],
			);
		}

		// size registered, but has 0 width and height
		if( $unset_disabled && ($sizes[ $_size ]['width'] == 0) && ($sizes[ $_size ]['height'] == 0) )
			unset( $sizes[ $_size ] );
	}

	return $sizes;
}

die( print_r( get_image_sizes() ) );

Now go to the site and you will see a similar array:

Array
(
	[thumbnail] => Array
		(
			[width] => 150
			[height] => 150
			[crop] => 1
		)

	[medium] => Array
		(
			[width] => 300
			[height] => 300
			[crop] =>
		)

	[medium_large] => Array
		(
			[width] => 768
			[height] => 0
			[crop] =>
		)

	[large] => Array
		(
			[width] => 1024
			[height] => 1024
			[crop] =>
		)

	[post-thumbnail] => Array
		(
			[width] => 825
			[height] => 510
			[crop] => 1
		)
)

How to disable the creation of copies?

To disable the default generation of copies, go to Settings > Media Files and set zero for the "large" and "medium" sizes.

Media settings page - leave only the necessary sizes

IMPORTANT: The thumbnail size is recommended to be left, because this size is used when creating a standard gallery and preview of uploaded files in the admin panel. If you remove it, the standard gallery will not work properly. And the media library window will load the originals of the files, which will severely slow down the admin panel.

Note: when displaying images in the media file uploader when attaching an image to a post, the medium size (medium) is used. But if it does not exist, the thumbnail is used. Therefore, the medium size can be safely disabled.

The medium_large size, created by default since version 4.4 cannot be disabled on the media settings page. To disable it, use the hook below. Or go to the hidden settings page example.com/wp-admin/options.php and set 0 in the setting medium_large_size_w:

Disable the medium_large size from the admin panel

#1 Disable in code

The other two copies are created by the theme. Usually, the theme code responsible for generating additional sizes can be found in the functions.php file. Look for sizes you saw via FTP (672, 1038, 576) and the function add_image_size(). It will look something like this:

add_image_size( 'homepage-thumb', 1038, 576, true );

Delete or comment out this line, this will disable the generation of the size 1038x576.

Creating copies can also be handled by the set_post_thumbnail_size() function, which looks like this:

set_post_thumbnail_size( 150, 150 );

It needs to be deleted or commented out.

#2 Remove registered sizes through the filter

In the WordPress 3.9. version, the remove_image_size() function appeared - it allows you to remove the specified thumbnail size (physical files are not deleted). Now, instead of deleting lines, you can disable unnecessary sizes by adding a filter to the functions.php file:

add_action( 'after_setup_theme', 'remove_plugin_image_sizes', 999 );

function remove_plugin_image_sizes(){
	remove_image_size( 'image-name' );
}

Here 'image-name' is the name of the size ('homepage-thumb' from the example above). The removal is hung on the after_setup_theme event, usually this event is enough. 999 means that the priority of the removal function execution is late, i.e., the function will be called after all others during this event.

#3 Disabling the creation of additional sizes through the filter

The options below may save you when no other option works. Both are almost the same. They do not disable the size registration, but simply remove it when it is received in the relevant parts of the code.

Option 1

This option is more complete because it disables sizes in several places, not just during image upload to the upload folder, as the second option does.

Also, image plugins use the get_intermediate_image_sizes() function to get intermediate sizes. With this option, such plugins will not see unnecessary sizes at all.

## disable thumbnail creation for specified sizes
add_filter( 'intermediate_image_sizes', 'delete_intermediate_image_sizes' );

function delete_intermediate_image_sizes( $sizes ){

	// sizes to be removed
	return array_diff( $sizes, [
		'medium_large',
		'large',
		'1536x1536',
		'2048x2048',
	] );
}
Option 2

This option disables the registration of specified sizes only when the image is uploaded to the site in the uploads folder.

// disable the creation of thumbnail files for specified sizes
add_filter( 'intermediate_image_sizes_advanced', function( $sizes ) {

	unset( $sizes['blog-large'] );
	unset( $sizes['blog-medium'] );
	unset( $sizes['tabs-img'] );
	unset( $sizes['related-img'] );
	unset( $sizes['portfolio-full'] );

	return $sizes;
} );

In this code, the names: 'blog-large', 'blog-medium', etc. - these are the names of registered thumbnail sizes that should not be created when the original image is uploaded.

#4 Change or disable *-scaled size

For more information on the *-scaled size, see this note.

# Change the maximum allowable image size by width/height
add_filter( 'big_image_size_threshold', function(){
	return 1600;
} );
# Cancel `scaled` size - limit the maximum size of the image
add_filter( 'big_image_size_threshold', '__return_zero' );

How to delete the original image after it's uploaded if a -scaled size has been created for it can be found at this link.

#5 Removal of old sizes (no longer needed files)

After disabling unnecessary sizes, physical files for already uploaded images will remain, so they should be deleted.

For this, you can use the wp-cli command wp media regenerate or the plugin Force Regenerate Thumbnails.

Conclusion

Before using the material from this article, make sure that the removed sizes are not used in the current theme, as thumbnails for posts, in the sidebar, or in other post types. Delete the created copies only when you are sure that they are not needed for the correct operation of the theme.

Topic plugins

Kama Thumbnail

My thumbnail creation plugin. With it, you can dynamically create the sizes you need in the theme. And upload only originals. That's what I do on all my projects.

Force Regenerate Thumbnails

When disabling thumbnail sizes, physical thumbnail files that are in folders are NOT deleted. Only the registered size is disabled, and the thumbnail with the disabled size will not be created during the next file upload. To clean up "old" files, use the "Force Regenerate Thumbnails" plugin. The plugin will fully recreate physical files according to the current thumbnail settings.