Why not removing image size not working?

Hi

I tried this code but they are not removed. Any advice? Thank you.

add_filter( 'intermediate_image_sizes_advanced', 'prefix_remove_default_images' );
// Remove default image sizes here. 
function prefix_remove_default_images( $sizes ) {
 unset( $sizes['small']); // 150px
 unset( $sizes['medium']); // 300px
 unset( $sizes['large']); // 1024px
 unset( $sizes['medium_large']); // 768px
 unset( $sizes['1536x1536 ']); // 768px
 return $sizes;
}

try to use intermediate_image_sizes | Hook | WordPress Developer Resources

add_filter( 'intermediate_image_sizes', 'remove_some_img_sizes' );

function remove_some_img_sizes( $sizes ) {
  $targets = ['small', 'medium', 'large', 'medium_large', 'large', '1536x1536'];

  foreach( $sizes as $k => $size ) {
    if( in_array( $size, $targets ) ) {
      unset( $sizes[$k] );
    }
  }

  return $sizes;
}

Hi @suabahasa
I implemented the code. Here is the result. It only removed ‘1536x1536 0 0’. Do you have any idea why i got 0 0 width and height? Thank you so much.
Screen Shot 2021-05-29 at 6.08.03 AM

try use both snippet code, the intermediate_image_sizes and intermediate_image_sizes_advanced hook.

Quoting from Image Sizes in WordPress (rudrastyh.com),

intermediate_image_sizes

This filter hook applies before intermediate_image_sizes_advanced and passes only one parameter — the array of image sizes names, for example: array('thumbnail', 'medium', 'large', 'custom_size').

intermediate_image_sizes_advanced

This filter runs after the image sizes names are converted into an associative array. You can use this filter the same way to unset image sizes by name, but you can use additional values that are passed to the filter.

at the last, verify if the file exists in the filesystem.

@suabahasa Thank you.