How to delete all product images in Magento 2?

First we need to understand how images are stored in the database and what are the tables involved that stores the image name, entity_id, attribute_id and attribute_code.

catalog_product_entity_varchar is used for storing product images value.

eav_attribute table

catalog_product_entity_media_gallery_value_to_entity that joins catalog_product_entity_media_gallery and catalog_product_entity tables.

NOTE : PLEASE TAKE BACKUP BEFOREHAND

Step 1 : Delete all the product image attribute code values. Where 87, 88 and 89 are the attribute_id for attribute_code image, small_image and thumbnail respectively.

DELETE FROM catalog_product_entity_varchar WHERE attribute_id IN (87,88,89);

Step 2 : All the catalog product gallery images are stored in these three tables. They need to be removed from the database. Use your favourite database management application.

SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE catalog_product_entity_media_gallery;
TRUNCATE catalog_product_entity_media_gallery_value;
TRUNCATE catalog_product_entity_media_gallery_value_to_entity;
SET FOREIGN_KEY_CHECKS = 1;

Step 3 : Log in to the Magento server as, or switch to, a user with permissions to write to the Magento file system.

cd <magento_root>/pub/media/catalog/product
rm -rf * 

Type y and press Enter.

Leave a Reply

Your email address will not be published. Required fields are marked *