6

We would like to remove all product images from the database and the file-system in Magento 2.2.8 shop.

There is a solution for Magento 1

Magento Delete all Product Images

Can this easily be adapted to Magento 2?

I am thinking about

  • rm -r pub/media/catalog/product/? pub/media/catalog/product/cache # keeping placeholder folder
  • And deleting from some tables in the database (which one) ?
  • Reindex

is this safe or are complications to expect?

We rather would not like to iterate over each product and save it, because of performance reasons.

5 Answers 5

10

Apart from:

catalog_product_entity_media_gallery

and

catalog_product_entity_media_gallery_value

tables, now Magento 2 has one more table

catalog_product_entity_media_gallery_value_to_entity,

which keeps track of removal of product in catalog_product_entity table, and relates this table to catalog_product_entity_media_gallery.

You need to check this table also.

One more table (catalog_product_entity_media_gallery_value_video) has been introduced in Magento 2, which keeps entries related to product videos.

Please review this table if you have any videos for products.

After the database operation, you can remove the image files from:

  • pub/media/catalog/product/ folder
  • pub/media/catalog/product/cache # keeping placeholder folder

That's all.

Note: Keep a backup of the database before doing any exercise on the database.

8

Based on mohit's answer:

truncate catalog_product_entity_media_gallery_value;
truncate catalog_product_entity_media_gallery_value_to_entity;
delete from catalog_product_entity_media_gallery;  # 9 sec
delete from catalog_product_entity_varchar where attribute_id in ( select 
     attribute_id from eav_attribute where attribute_code like "%image" and 
     entity_type_id = 4 ); # 23 sec in our case (20000 Products)
3
  • Alex missed the removing query for thumbnail attribute.
    – sergei.sss
    May 19, 2021 at 18:44
  • What do you mean? Feel free to post another answer :)
    – Alex
    May 20, 2021 at 7:03
  • 2
    He means that there are other attributes for other image roles as well, the query could be modified so instead of using a like "%image" to something like this. WHERE attribute_code IN ( 'image', 'small_image', 'thumbnail', 'weltpixel_hover_image' ) AND entity_type_id = 4 Oct 16, 2021 at 7:17
1

I know it's too late to answer, but I guess this will help others as it helped me. I have slightly different requirements for deleting the images for specific products. So I have prepared the below query to delete it from the database and folder structure.

Query:

DELETE FROM catalog_product_entity_media_gallery WHERE value_id IN (
SELECT cpemgv.value_id FROM `catalog_product_entity_media_gallery_value` as cpemgv
LEFT JOIN catalog_product_entity as cpe on cpe.row_id = cpemgv.row_id
WHERE cpe.entity_id IN (101, 102));

I agree with @mohit's answer, but since the database tables have a relation it's okay to perform the delete operation only on the catalog_product_entity_media_gallery table for images.

And I will suggest taking the product dump from Magento before performing this operation so that you will have the image path of the products. Then use the terminal to delete the images from the folder structure.

1

There is also API way to remove and change images based on products SKU, Just pass your desired SKUs through a loop

https://adobe-commerce.redoc.ly/2.4.6-admin/tag/productsskumediaentryId

0

Try this one

truncate catalog_product_entity_media_gallery_value;
truncate catalog_product_entity_media_gallery_value_to_entity;
delete from catalog_product_entity_media_gallery;  # 9 sec
delete from catalog_product_entity_varchar where attribute_id in ( select 
     attribute_id from eav_attribute where attribute_code IN ( 'image', 'small_image', 'thumbnail', 'swatch_image', 'media_gallery' ) AND entity_type_id = 4 );

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.