Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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; 
} 
?>
share|improve this answer

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.

share|improve this answer
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. –  Brady Jan 26 '12 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 Jan 28 '12 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. –  Brady Jan 28 '12 at 10:01
    
AH of course, will check that out, thanks. –  Cristian Jan 28 '12 at 10:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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