Exclude main image from Magento product gallery

Exclude main image from Magento product gallery

October 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Exclude main image from Magento product gallery

Excluding Main Images from Magento Product Galleries

As an e-commerce developer working with Magento, you may find yourself in situations where you need to exclude certain images from product galleries, especially when dealing with large inventories of products. In this post, we’ll explore a solution for excluding the main image (base, small, and thumb) from the lightbox gallery.

Understanding the Database Structure

Magento 1.4.0.1 uses the catalog_product_entity_media_gallery table to store product image data. The relevant columns here are:

  • entity_id: The product ID
  • file: The filename of the image
  • exclude: A flag indicating whether the image should be excluded

Our goal is to update this table to set the exclude flag to 1 for all images that have their base, small, and thumb flags set.

Writing the SQL Query

Before we begin, it’s essential to backup your database. A single typo in a SQL query can cause serious data loss. Here’s an example of how you might structure your UPDATE query:

UPDATE catalog_product_entity_media_gallery AS gallery
SET gallery.exclude = 1
WHERE EXISTS (
    SELECT *
    FROM catalog_product_entity_varchar AS attributes
    WHERE attributes.entity_id = gallery.entity_id
        AND attributes.store_id = 0
        AND (attributes.attribute_id IN (49, 50, 51)
               OR attributes.attribute_id IN (87, 88))
);

Let’s break down this query:

  • The outer UPDATE clause targets the catalog_product_entity_media_gallery table.
  • We’re setting the exclude column to 1 for rows that match the conditions in the inner EXISTS subquery.
  • The inner query checks if the product has an attribute ID of 49, 50, or 51 (which correspond to base image flags), or attribute IDs 87 and 88 (for small and thumb images).
  • We’re only considering rows where the store ID is 0 for consistency across all stores.

Implementing the Solution

To run this query in your Magento installation:

  1. Log into your MySQL server using a client like phpMyAdmin or the command line.
  2. Select your database containing the Magento data.
  3. Copy and paste the SQL code above into the query editor.
  4. Click “Go” to execute the query.

Best Practices

While this solution works, it’s important to keep a few things in mind:

  • Always backup your database before making significant changes.
  • If you’re not comfortable writing or modifying complex SQL queries, consider hiring a professional developer.
  • After running the query, double-check that the images have been excluded by viewing a product page.

Conclusion

Excluding main images from Magento galleries requires a careful update of the catalog_product_entity_media_gallery table. By using an appropriately structured SQL UPDATE query, you can efficiently mark all relevant images as excluded without manual intervention.

Remember to always prioritize data safety and accuracy when working with e-commerce databases. If you’re looking for professional assistance or need more complex customizations, don’t hesitate to reach out to our team at Laravel Company. We specialize in Magento development and can help ensure your site remains secure and optimized.

Magento 1 Catalog Database

Our network