How do you exclude all product images of a product,
i.e. the Exclude box of every image being ticked on the Images tab in the Magento backend.

I'm bulk adding new images to products using $product->addImageToMediaGallery() but before adding the new pictures I want to exclude the already existing images.

I want exclude them and not remove them so they can easily be restored in the future if needed.

  • Just to get sure I understand your question: Given a product with already exiting images a.jpg and b.jpg. You upload c.jpg and a.jpg. Expected behaviour is to upload both images and exclude the second a.jpg, so you have a media gallery with a.jpg, b.jpg and c.jpg and in the backend you have 4 images with the second a.jpg (renamed to a_1.jpg by Magento by default) with checkbox "Exclude" checked? – HelgeB Feb 8 '19 at 7:33
  • The expected behaviour is to exlude the images that are already in Magento. The new uploads replace the old ones, filenames don't have to be renamed. Expected behaviour: Already existing images: a.png, b.png. New uploads: c.png, d.png. Result: a.png and b.png are exluded, c.png and d.png are not. Right now all four images are enabled and I wish to exlude a.png and b.png. Or basically I'm asking how to exclude an image in general. – SuperDicq Feb 8 '19 at 11:41

You can exclude any product image for a given product by using the updateImage method from the model Mage_Catalog_Model_Product_Attribute_Backend_Media if you know the filename of the image you want to exclude (filename is the complete path beginning with the slash below media/catalog/product). For example with the following code you can exclude all existing images from the media gallery for the product with id = 12345:

$product_id = '12345';

$product = Mage::getModel('catalog/product')->load($product_id);
$attributes = $product->getTypeInstance(true)->getSetAttributes($product);

if (isset($attributes['media_gallery'])) {
    $mediaGalleryAttribute = $attributes['media_gallery'];
    $media = $mediaGalleryAttribute->getBackend();
    $mediaGalleryData = $product->getData('media_gallery');
    if (isset($mediaGalleryData['images']) && is_array($mediaGalleryData['images'])) {

        foreach ($mediaGalleryData['images'] as $image) {
            $media->updateImage($product, $image['file'], ['exclude' => 1]);

Running this code before your $product->addImageToMediaGallery() call, should do what you want - maybe it's usefull to add a little more error handling.

There is of course a faster way just by updating the catalog_product_entity_media_gallery_value table, but the approach above is probably the better one for your requirement.

Just to be complete, here the SQL statement for the fastest and simplest solution since you can exclude all images for all your products in one statement:

update catalog_product_entity_media_gallery_value set disabled = 1 
where value_id in (
      select value_id from catalog_product_entity_media_gallery 
      where entity_id in (12345,12346)  -- list your product_ids here
| improve this answer | |
  • Your first solution executed for me without errors but did not update the records and I have no idea why. – SuperDicq Feb 12 '19 at 15:45
  • However, I have fixed my issue by using your raw query. Thanks – SuperDicq Feb 12 '19 at 15:46

Please find my alternate solution for exclude image if you prefer to keep your code Simple.

  $product = Mage::getModel('catalog/product')->load(1234); //Your Product ID Here
  $mediaApi = Mage::getModel("catalog/product_attribute_media_api");    
  $images = $mediaApi->items($product->getId());  
  foreach ($images as $image) {
     $data = $image;
     $data['exclude'] = 1;
| 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.