Remove images (files) that were generated when using add_image_size()

I have added an image size using this:

add_image_size('property-featured', 484, 393, true);

It’s worked very well for the past year and has generated about 1GB of images in that size, but this image size is no longer needed. I want to clear up the images that have been created over the year in this size.

Read More

So far I have removed that line of code but it doesn’t clean up the created images.

What is the correct way to remove those images?

Related posts

Leave a Reply

2 comments

  1. I found a plugin that does this for me: Additional image sizes (zui)

    When you delete an image size that size will not be created for all NEW images you upload.
    However, images created for deleted sizes still exist on the server as well as the image attachment metadata for those sizes.
    This feature will physically delete those images from the server as well as the image attachment meta data for those sizes.
    Use this feature at your own risk. There is no undo.

    Update

    While the plugin worked wonders and did a lot of cleanup it couldnt cleanup the files left behind by images that had been removed from WordPress in the past. I used this home brew script to clean up some of the left over images:

    <?php
    $files = find_all_files("/home/****/public_html/wp-content/uploads");
    $files2 = array();
    
    foreach($files as $key => $file) {
        if(1 == preg_match("#150xd+.jpg$#", $file)) {
            $files2[] = $file;
            unlink($file);
        }elseif(1 == preg_match("#300xd+.jpg$#", $file)) {
            $files2[] = $file;
            unlink($file);
        }elseif(1 == preg_match("#d+x300.jpg$#", $file)) {
            $files2[] = $file;
            unlink($file);
        }elseif(1 == preg_match("#d+x150.jpg$#", $file)) {
            $files2[] = $file;
            unlink($file);
        }elseif(1 == preg_match("#d+x1024.jpg$#", $file)) {
            $files2[] = $file;
            unlink($file);
        }
    }
    
    
    print_r($files2);
    
    
    function find_all_files($dir) 
    { 
        $root = scandir($dir); 
        foreach($root as $value) 
        { 
            if($value === '.' || $value === '..') {continue;} 
            if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;} 
            foreach(find_all_files("$dir/$value") as $value) 
            { 
                $result[]=$value; 
            } 
        } 
        return $result; 
    } 
    ?>
    
  2. I use http://wordpress.org/extend/plugins/regenerate-thumbnails/ to recreate my thumbnails, as far as I am aware it removes all image sizes then regenerates them. It could be a slow one depending on how many images + image sizes are reigstered.

    Another way would be to do this via SSH and find them with a command like:

    find ./uploads/*  -iname '*-484x393.*' -ls
    

    Then run a command like:

    find ./uploads/*  -iname '*-484x393.*' -exec rm {} ;
    

    Please make sure you back up everything before running such a command.