How to delete resized (cropped) image uploads and prevent future resizing?

A majority of the answers covered how to stop creating future default image sizes but this doesnt account for creating any custom sizes in your theme but here is another solution to add to functions.php:

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

and you can also turn off future default image generation by setting the images to zero:

enter image description here

but to remove the images other then the originals I’ve ran into your same issue when I forgot to set it to not do it and what I did was:

  • Download all the photos locally using an SFTP service, I love Transmit (paid) but you can use something like Filezilla (free) .

  • Download all the files to a directory.

I’m on a Mac but any terminal that allows bash will work. I coded a simple bash script:

# !/bin/bash

USERNAME=vader
DIRECTORY="/Users/$USERNAME/desktop/question240765"
for imageWithSize in $(find "$DIRECTORY" -type f -regex '.*/[a-z-]*-[0-9].*.txt$'); do
    cd $DIRECTORY
    echo rm $imageWithSize
done

The folder is located on my desktop, and for the question I named it question240765. I used .txt files to test this but you can change it to .jpg. I saved it as a bash file image_dust.sh so that it will allow me to modify or enhance later down the road. Run the script first with the echo and you could even dump it to a file with changing the line:

echo rm $imageWithSize 

to:

echo rm $imageWithSize >> result.txt

which will log everything to the file result.txt and allow you to browse it before really removing them. If all is well change that line to:

rm $imageWithSize

If you’re curious here is what the regex does:

  • [a-z-]* looks for filenames like foo-bar or fo-fo-bar. if you have uppercase letters in your name use [A-Za-z-]*
  • -[0-9] after the filename it looks for the remaining - (dash) with a number [0-9]
  • .*.txt looks for anything after the first digit to the end of the name with the extension.

After completing the scripting and running it. You could blow everything away on your site and re-upload the images. If you’re worried about file size I would even use imagemagick but I prefer sips to reduce the compression size of the images.

Leave a Comment