Stop generating additional image sizes in WordPress

Whenever you upload image to media library, WordPress will automatically generate additional images with 3 default image sizes and store them separately.

Then your theme and plugins will generate some additional image sizes too. In my case total of 13 additional image sizes were created. 🤯

It’s actually good to have additional image sizes to show proper image sizes based on the page or elements. But I don’t needed them, all I needed was the actual uploaded image.

I searched Google but none of them had any actual or proper way to stop generating them. Thus I created one. Just copy this into your theme’s functions.php and you’re good to go.

/**
 * Remove the default image sizes.
 */
	add_filter( 'intermediate_image_sizes_advanced', function( $sizes ) {
		unset( $sizes['thumbnail'] );
		unset( $sizes['medium'] );
		unset( $sizes['large'] );
	});

/**
 * Remove any other image sizes generated by themes or plugins
 * Uses 'wp_loaded' hook which is fired when everything fully loaded and instantiated.
 */
	add_action( 'wp_loaded', function() {
		foreach ( get_intermediate_image_sizes() as $image_sizes ) {
			remove_image_size( $image_sizes );
		}
	});

Removing image size this way is completely safe. If WordPress can’t find any specific image sizes then it will automatically show the original one.

Share your thoughts by leaving a comment below.