4

I want to delete all the resized images while leaving the original image. I have more than 20 GB of unused data taking up room on the server. For example:

  • first-image-name.jpg
  • first-image-name-72x72.jpg
  • first-image-name-150x150.jpg
  • first-image-name-250x250.jpg
  • first-image-name-300x300.jpg
  • first-image-name-400x400.jpg
  • first-image-name-1024x1024.jpg
  • second-image-name.jpg
  • second-image-name-72x72.jpg
  • second-image-name-150x150.jpg
  • second-image-name-250x250.jpg
  • second-image-name-300x300.jpg
  • second-image-name-400x400.jpg
  • second-image-name-1024x1024.jpg

Is there a way to delete all the resized images and disable creating such ones in the future?

  • 3
    You can probably clear the extra sizes by clearing out global $_wp_additional_image_sizes; and removing calls to add_image_size() - as for deleting existing files maybe use wordpress.org/plugins/regenerate-thumbnails – jgraup Sep 28 '16 at 0:37
  • 1
    That plugin doesn't delete redundant files in my experience. @jgraup – Andy Macaulay-Brook Sep 28 '16 at 7:48
  • @AndyMacaulay-Brook, May be this can do – bravokeyl Oct 7 '16 at 3:21
2

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.

1

No idea, how to remove existing images. But you can stop generating image sizes for new images you are going to upload.

If you like to write codes: Place this code snippet in your theme's functions.php file-

add_filter( 'intermediate_image_sizes_advanced', 'wpse_240765_image_sizes' );

function wpse_240765_image_sizes( $sizes ){
    $sizes = array();
    return $sizes;
}

Or with a plugin: Use this plugin- https://wordpress.org/plugins/image-sizes/

It'll give you the same output. You can even choose which of the image sizes you want to be prevented from creating. enter image description here

1

If coding is not your strongest point you may want to look at a plugin solution.

What I have in mind is a suggestion of a couple free plugins residing in the WordPress repository:

I must admit that I have not yet used the first one - Media Cleaner - so I cannot provide you with a solid opinion.

I have used though the second one - Optimize Images Resizing - and had the expected results.

Points to always look for in any case:

Before attempting anything,

  • I cannot stretch enough the importance of creating a full backup of your WordPress installation (both your uploads folder and your dB).
    I do understand that we are talking about 20GB but a backup is essential, especially in this unique situation.

  • This procedure is time consuming as well as heavy on the server.
    It is wise to put your site in the maintenance mode.

  • Delete orphan attachments

After all is done use your favorite Regenerate Thumbnails plugins to get your WordPress/themes image sizes back as well as install EWWW Image Optimizer and Imsanity. This combination will guarantee optimized, resized, only necessary images.


Plugin specific points.

Media Cleaner

  • "Files detected as un-used are added to a specific dashboard where you can choose to trash them. They will be then moved to a trash internal to the plugin. After more testing, you can trash them definitely".
  • Some plugins may clash with it. If that happens and assuming you are on maintenance mode, deactivate all and do your cleaning.

Optimize Images Resizing

  • "TO REMOVE image sizes generated prior to activating the plugin, visit the Settings -> Media and use the button under "Remove image sizes" to perform the cleanup".
  • Read this support thread: Urgent questions on processing existing images

Thumbnail Cleaner

  • Includes functions like: Creating backups of your uploads, analyzes your uploads directory, giving you an overview how many original files and thumbnails there are, restores backups in case you have lost a file that was not supposed to be deleted.
  • Read this support thread: Backup only original image

Please let us know how you achieved the best results.

1

To remove all superfluous image files, we'll have to loop through all posts, find the attachments to those posts, establish which attached image sizes are no longer needed and remove those files. Here we go.

First, get all (custom) posts and pages and loop through them:

$the_query = new WP_Query (array ('posts_per_page' => -1, 'post_type' => 'any'));
foreach ($all_posts as $post) { ... }

Next for $post get all the attached images and get a list of all media sizes. Then loop through them:

$images = get_attached_media ('image',$post); // returns array of post objects
$sizes = get_intermediate_image_sizes(); // returns array of identifying strings
foreach ($images as $image) {
  foreach ($sizes as $size) {
    ...
    }
  }

Now for this image and size, find the url and delete the file

$att = wp_get_attachment_image_src ($image->ID,$size); returns array of file properties
wp_delete_file ($att['url']);

I'd give this code a thorough test before applying it... With 20GB in images it could also take a while to execute.

1

If you have SSH access then you can run the following commands to list and remove the resized images. Later you can rebuild them.

List all resized images.

# List all resized images.
# Usage: lrimg site.ext
function lrimg() {
  clear
  THE_PWD=$PWD
  cd ~
  cd /var/www/"$1"/htdocs/wp-content/uploads/
  find . -regextype posix-extended -regex ".*-[[:digit:]]{1,9}x[[:digit:]]{1,9}(@2x)?.(jpg|jpeg|png|eps|gif)" -type f
  cd $THE_PWD
}

Remove all resized images.

# Remove all resized images.
# Usage: lrimgrm site.ext
function lrimgrm() {
  clear
  THE_PWD=$PWD
  cd ~
  cd /var/www/"$1"/htdocs/wp-content/uploads/
  find . -regextype posix-extended -regex ".*-[[:digit:]]{1,9}x[[:digit:]]{1,9}(@2x)?.(jpg|jpeg|png|eps|gif)" -type f -exec rm {}  \;
  cd $THE_PWD
}

Be careful here. Keep your backups ready.

P.S. My server has an NGINX setup which is why the site paths are /var/www/"$1"/htdocs/ $1 being the parameter which is the site name. You can modify it to match your path.

0

1) To stop WordPress from creating multiple image sizes upon uploading of an image, you can accomplish this without using a plugin.

Navigate to Settings > Media in the admin section.

By setting the sizes to 0 you will disable WordPress from creating default image sizes and only the original image will be stored.

enter image description here

2) To reduce the 20GBs of images you have on your site, I would simply SFTP into your server and manually delete the different sized images. From the root directory of a WordPress install, navigate to wp-content/uploads/<yyyy>/<mm>/ and you will find all of your images and media uploads. You want to be sure to leave the original image, or else you are simply deleting the entire image. Any image file name with an extra - ###x###.jpg is an auto generated image. The original image would not have this in the file name.

As an aside - to help you with your images I would check out WPMU DEV's WP-Smush plugin.

WP-Smush - Image Optimization

It's really great and when you upload images it automatically uses their servers to crunch and compress images before storing. It also can size down large resolution images to a reasonable maximum size. You really shouldn't have to display an image that is bigger than 2048x2048.

  • Ok ... how to SFTP and delete additional resized images ??? – Mohamed Gamal Sep 28 '16 at 19:21
  • This is the program I use to SFTP into my server. winscp.net/eng/download.php It basically lets me browse the contents of the entire wordpress install, and I can edit any file in my text editor; I can easily add and remove files with this as well. It will depend on your hosting provider to get setup and logged in with SFTP. – italiansoda Sep 28 '16 at 20:34
  • Or Cyberduck, which has an OSX and Windows version. cyberduck.io/?l=en – italiansoda Sep 28 '16 at 20:45
  • Can it done using FTP Client ? – Mohamed Gamal Sep 28 '16 at 20:45
  • That's basically the same thing. The S simply standing for Secure FTP. – italiansoda Sep 28 '16 at 21:06
0

The flowing steps should do it:

1)You should start by removing any unneeded image sizes from your relevant files e.g. functions. PHP in your theme and any plugins if the exist (e.g. rudderless).

2)Next you can delete all required image sizes via ftp.

3)Then you need to regenerate the thumbnails (e.g. all the image sizes that Wordpress is being told to create.) You can use any 'Regenerate thumbnails' plugin from the WP repo.

Alternatively try this: 1) Same as step 1 above

2) There are a couple of plugins in the plugin directory(repo) which will delete all WP thumbs and then only generate the thumbs when they are called for the first time on any given page on your website. On subsequent request for the image, the image will already exist and be served as normal. The benefit of this is that thumbs are only created for images/sizes that are actually required by your site. However, images called in non-standard ways may not get generated.

If you want to reduce additional overhead from excessively large original images you can use 'imsanity' plugin to reduce the physical file size of these originals down to a size of your choice. It's a great tool for sites with user submitted images.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.