6

I'm trying to understand how M2 handle images of deleted products. If I delete a product (by admin or import/delete), all associated images in the media/catalog/product folder are still here.

In M1 and Magento Image Clean installed, I was able to remove all images of a deleted product. The same extension Magento 2 Image Clean for M2 fetch only 1 unused image.

After many reading, I found this Magento 2, How to delete unused images using PHP script. The solution seems to use the php bin/magento eav:media:remove-unused command of magento-hackathon/EAVCleaner but still only 1 image found (same that the previous extension).

I try a script on my own who scan the catalog_product_entity_media_gallery table, then scan the media/catalog/product folder to look for the difference but there is not. After digging into the database, I just realize that M2 keep records of old deleted product images in the DB.

Question is: how do you manage disk usage with all these useless images ?? My store has almost 4 collections/year and around 11.000 products to change... in only 6 months media/catalog/product folder size is around 6GB (without the cache) and still growing.

PS: I'm on a 2.1.9 version

2 Answers 2

8

After many hours of reading, digging and testing, I finally understood how Magento2 handle products images.

  • catalog_product_entity_media_gallery table content records of all images uploaded for products
  • catalog_product_entity_media_gallery_value_to_entity table content relation between products ID and associated images

Every extensions I found for cleaning the media/catalog/product folder does not work as I expect. (links in question)

They compare the media folder with the catalog_product_entity_media_gallery table.
=> Found only images without any records in the db but not deleted products one

I still don't unnderstand why Magento2 doesn't remove images while deleting product.

Based on EAVCleaner modified, I developed a new Command Line with my needs, result :

Found 1,193.83 MB unused images in 6651 files

Flushing the images cache, Its finally 4,5gb saved after only 1 year in production...

I share my code on Github M2DeletedProductImage for those who are looking for the same behavior. (not 100% safe, still don't really know possible issues of deleting old images)

1

Thank you for the M2DeletedProductImage solution, it worked perfectly for me.

Found 354495 files 23,698.23 MB

However this tool did not fix another issue I was looking the solution for. I have some entries in DB tables:

catalog_product_entity_media_gallery

catalog_product_entity_media_gallery_value

catalog_product_entity_media_gallery-value_to_entity

for products I deleted long time ago and also the images are not present in the pub/media/catalog/product.

I also checked the catalog_product_entity and this products do not exist.

In catalog_product_entity the smallest number for entity_id (Product ID) is 2168 (The same in Magento Admin) but in at least the 3 other tables mentioned above I have entries for more than 100 entity_id(s) from 1 to 2167.

How can I fix this?

1
  • sorry no ideas... I'm just a self made, hours of digging but far away from a developper.
    – cap340
    May 31, 2019 at 18:18

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.