1

I want to programmatically remove ALL product images as I am going to programmatically re-add all images. I've managed to remove gallery images (thanks to https://stackoverflow.com/questions/5709496/magento-programmatically-remove-product-images) but I can't get rid of that main image.

I can delete it from the file system obviously but that still leaves a placeholder (with broken image).

Any tips on how to remove the image without removing the product would be very appreciated.

3 Answers 3

3
    $attrCode         = 'media_gallery';
    $mediaGalleryData = $product->getData($attrCode);
    if (isset($mediaGalleryData['images'])) {
        foreach ($mediaGalleryData['images'] as &$image) {
            $image['removed'] = 1;
        }
        $product->setData($attrCode, $mediaGalleryData);
        $product->save();
    }
2
  • Thanks for the very fast answer mageUz. Unfortunately it doesn't seem to solve the problem, at least not for me. The main image is still not removed. In one example I had three images (one main and two in the gallery). The two images in the gallery are removed but not the main image. I can see that your code enters the for loop three times but somehow the "removed" is ignored for the main image.
    – Klistrian
    Oct 21, 2013 at 19:54
  • Together with another response I used your code for a working solution. See my own answer to my question. Thanks!
    – Klistrian
    Jun 10, 2015 at 14:04
3

Maybe you should first reset all the image selections to 'no_selection' then try to remove the images.
This should do it:

//values for image attributes
//add others if you have
$values = array(
    'image'=>'no_selection',
    'small_image'=>'no_selection',
    'thumbnail'=>'no_selection',
);
//get all product ids
$productIds = Mage::getModel('catalog/product')->getCollection()->getAllIds();
//set new values.
Mage::getSingleton('catalog/product_action')->updateAttributes($productIds, $values, 0);

After running this, you can try the solution from the link you mentioned.

2
  • Also realised it wouldn't delete the image unless it was removed from all store fronts. So I now run it on all store fronts first and finally the admin. Problem is it throws an exception when trying to save a product on a non-admin store. PHP Fatal error: Uncaught exception 'Mage_Eav_Model_Entity_Attribute_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1874-0-3-1' for key 'CC12C83765B562314470A24F2BDD0F36'' in /.../html/app/code/core/Mage/Core/Model/Config.php:1348 Any ideas on that @Marius or anyone else?
    – Klistrian
    Oct 31, 2013 at 14:05
  • Solved the duplicate entry issue by applying @Marius code for each store front before attempting the delete. See my complete solution in the separate answer I posted.
    – Klistrian
    Jun 10, 2015 at 10:48
3

After trying out a bunch of different versions I landed in this one which works for me every time. The key for me was removing all image settings for each store before I could completely delete the images from the system.

<?php
require_once 'app/Mage.php';
Mage::app('default');


// Retrieve all products. You could populate the array from, for example, a list of SKUS if you only want to delete images on a subset
$products = Mage::getModel('catalog/product');
$prodIds=$products->getCollection()->getAllIds();
$nrOfProducts = count($prodIds);

$values = array(
    'image'=>'no_selection',
    'small_image'=>'no_selection',
    'thumbnail'=>'no_selection',
);

//Populate this array with IDs of ALL your store fronts
$store_fronts = array(1,2);

foreach ($store_fronts as $store_front)
{
    Mage::app()->getStore()->setId($store_front);
    Mage::getSingleton('catalog/product_action')->updateAttributes($prodIds, $values, $store_front);
}
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::getSingleton('catalog/product_action')->updateAttributes($prodIds, $values, Mage_Core_Model_App::ADMIN_STORE_ID);

$counter = 0;
foreach($prodIds as $productId) {

    ++$counter;

    $products = Mage::getModel('catalog/product');
    $product = $products->load($productId);

    $attrCode         = 'media_gallery';
    $mediaGalleryData = $product->getData($attrCode);
    if (isset($mediaGalleryData['images'])) {
        foreach ($mediaGalleryData['images'] as &$image) {
            $image['removed'] = 1;
        }
        $product->setData($attrCode, $mediaGalleryData);
        echo "Saving product ".$name." (".$counter." / ".$nrOfProducts.") \n";
        try{
        $product->save();
        } catch (Exception $e)
        {
           echo "Exception saving ".$name.", skipping\n".$e->getMessage()."\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.