I want to mark some of the product images as excluded against some products. I tried this link http://www.magecoders.com/blog/2013/05/programmatically-exclude-product-images-in-magento/ so far. But didn't work.

3 Answers 3


I had the same issue. I managed to exclude the images by removing them using the same code but with a small change. Before doing any of the following, BACKUP your database.

Change this


To this


It will remove the image from displaying in admin, but the actual image will remain under media folder.

Regards, Daniel


Try this code:


class ITEMS
    public function index()    
        $productIds = Mage::getResourceModel('catalog/product_collection')->getAllIds();
        if(is_array($productIds)) {
            foreach($productIds as $productId) {
                $product = Mage::getModel('catalog/product')->load($productId);
                //get all images
                $mediaGallery = $product->getMediaGallery();
                $attributes = $product->getTypeInstance(true)->getSetAttributes($product);
                $mediaGalleryAttribute = $attributes['media_gallery'];
                //if there are images
                if (isset($mediaGallery['images'])){
                    //loop through the images
                    foreach ($mediaGallery['images'] as $image){
                        $mediaGalleryAttribute->getBackend()->updateImage($product, $image['file'], array('exclude' => true));
                    echo "Product ".$product->getSku()." images excluded <br>";

$obj = new ITEMS();

I am Successfully able to exclude the images by simple below code.

$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$this->_productSku);

if ($product->getId()){
     $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
     $items = $mediaApi->items($product->getId());
     foreach($items as $item){
         $mediaApi->remove($product->getId(), $item['file']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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