0

I inherited a WordPress site from a previous developer who had gone a little crazy with image sizes. As I was uploading it to the server I noticed there were at least 10 image sizes for every image. This resulted in a nice and long 9 hour upload time.

On the site, I reckon only 3-4 of these image sizes are actually being used.

My question is, is there a way to delete all images of a particular size?

2 Answers 2

0

Yap, a similar things happened to us once. And one of our senior developer and my teacher, Ms. Tahmina Akter, made the following code for us. Though it's too rough, and there are some deprecated PHP functions too (I fixed them and updated the code), but it works.

CAUTION: Handle it with PROPER CARE, because it's DEVASTATING.

How the code works:

  1. As it's a page template, place the file within your theme, add a new Page, and choose the template there
  2. Change $_DELETE_IMAGES = false; to true to trigger the code.
  3. $filename = $up_dir['basedir'] . '/2013/12/'; here, mention each of the folder of your /uploads/ directory, from where you want to delete bunch of images
  4. As WordPress stores all the image sizes with a common format, like: filename-WxH.ext, so we are targeting the width parameter, and ignoring the height portion, and saying if ( preg_match( '/\.(jpg|png|jpeg|gif)$/', $file ) ): match hyphen and the width we mentioned and the height can be whatever, and then proceed to the next step
  5. Now open the WordPress page once, and the code will be executed, and the rest of the code will delete the files from the /uploads/YYYY/MM/ directory

Each reload will execute the code once. So we hope each execution will delete all the bunch of images, but if till there are other unnecessary image sizes, you can go to Step#3 and change the sizes to that particular sizes and reload the page again to run the code to delete.

<?php
/**
 * Template Name: Remove Image
 */

$_DELETE_IMAGES = false;

if( $_DELETE_IMAGES ) {

    $upload_dir   = wp_upload_dir();

    $target_path  = $upload_dir['basedir'] .'/2018/03/';
    $images       = array();
    $sized_images = array();

    // Grab all the images first (jpg, png, jpeg, gif).
    $dir = opendir( $target_path );
    while ( $file = readdir( $dir ) ) {
        if ( preg_match( '/\.(jpg|png|jpeg|gif)$/', $file ) ) {
            $images[] = $file;
        }
    }

    // Keep images only of the defined sizes.
    while (sizeof($images) != 0){
        $img = array_pop($images);
        if ( preg_match("~\-300x~",$img) || preg_match("~\-624x~",$img) || preg_match("~\-570x~",$img) ) {
            $sized_images[] = $img;
        }
    }

    // Now iterate thorough grabbed images and delete/unlink() 'em
    while ( sizeof($sized_images) != 0 ) {
        $target_image = array_pop( $sized_images );
        $the_file     = $target_path . $target_image;

        if ( file_exists($the_file) ) {
            unlink($the_file); // delete the file
            printf( "File %s has been deleted.<br>", $the_file );
        } else {
            printf( "File %s cannont be deleted, because it&rsquo;s not existed.<br>", $the_file );
        }
    }

} else {
    echo 'Deletion of image is currently off';
}
0

Im using a plugin to regenerete all uploaded photos and delete all of them not registered on functions.php or any other file to initialize the file sizes. the pluglin is named Force Regenerate Thumbnails (link here) It will save storage. So you need to unregister the lines for undesired image sizes.

2
  • When to delete, how do you find all the unnecessary image sizes from a haystack? Commented Mar 29, 2015 at 7:43
  • functions.php have registered all image sizes will be created, for example 200px, 400px, 640px, 800px, 1200px if any of this sizes is removed from functions.php from these time the image won´t be created anymore, but i will remain inside the upload folder all created previusly. So with the regenerate plugin it will regenerate all again except those images that you delete from function.php and it will delete all files created previously, for example I dont need 400px and 1200px, my Function.php only register 200px, 640px, and 800px. With regenerate will delete all 400px and 1200px. :) simple.. Commented Mar 29, 2015 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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