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
Best Answer
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 productscatalog_product_entity_media_gallery_value_to_entity
table content relation between products ID and associated imagesEvery 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 thecatalog_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 :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)