-1

We have two different situations.

  • Out of stock products that have many images that we need to delete the extras without the base image
  • Deleted products that still have images in the media folder

We need to remove those images in a small code that can do it fast enough without doing any damage.

0

You can validate which images are currently assigned to any product in your catalog by reviewing catalog_product_entity_media_gallery database table. For instance...

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = $readConnection->select()
    ->from($resource->getTableName('catalog_product_entity_media_gallery'))
    ->group('value_id');

$imagesInUse = array();
$results = $readConnection->fetchAll($query);
foreach ($results as $item){
    $imagesInUse[] = $item['value'];
}

Now you have all images in use in $imagesInUse array, so you could iterate through media/catalog/product subfolders (ignoring cache and/or not relevant folders in your project) & delete any image not present in your array

| improve this answer | |

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.