7

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.

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?

2 Answers 2

9

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("#150x\d+\.jpg$#", $file)) {
        $files2[] = $file;
        unlink($file);
    }elseif(1 == preg_match("#300x\d+\.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.

3
  • 2
    At first I thought that was a good idea but when I looked into what data is stored it turned out that there is a whole heap of meta data stored in the DB for each resized image. Deleting the images this way will leave the data behind which is not what I'm after on a production site.
    – Scott
    Commented Jan 26, 2012 at 19:50
  • I took a glancing look into the WordPress meta tables etc and code base, and couldn't see what meta data you were talking about? Could you point me to what you were seeing? I might write a plugin over the weekend for this, as it's quite a nice little idea. :)
    – Cristian
    Commented Jan 28, 2012 at 8:40
  • 1
    No need to make a plugin as there are a fair few already. One I've already mentioned. But to find the meta data I'm talking about: Find an image attachment ID. Then look in the postmeta table and take a look at all the rows that are attached to that attachment. One of the rows (cannot remember name) has a tonne of serialized data in it. That data contains paths to all the resized images and what resulting size they are.
    – Scott
    Commented Jan 28, 2012 at 10:01

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