I want to delete all images from a few products via script. Here is what I got:

$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$items = $mediaApi->items($product->getId());

foreach($items as $item) {
  $mediaApi->remove($product->getId(), $item['file']);

This works for every image which is in the gallery except one. I just can't delete it. I am pretty sure it is because the last product image is stored for a specific store view. But calling


at the beginning does not help either. What can I do?


Here are complete steps to remove product images:

Step 1:

/***Load Products You want to delete product images***/
$products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id', array('gt' => 500));

Step 2:

$mediaApi = Mage::getModel("catalog/product_attribute_media_api");

Step 3:

foreach($products as $product) {
    $productId = $product->getId();
    $_product = Mage::getModel('catalog/product')->load($productId);
    $items = $mediaApi->items($_product->getId());
    if (count($items) > 0) { 
        foreach($items as $item) {
            $mediaApi->remove($_product->getId(), $item['file']);
        echo 'Product ID: '.$product->getId() . " images removed. \n";
    } else { 
        echo 'Product ID: '.$product->getId() . " image not exist. \n";

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.