netz98 / n98-magerun

The swiss army knife for Magento developers, sysadmins and devops. The tool provides a huge set of well tested command line commands which save hours of work time. All commands are extendable by a module API.
http://magerun.net/
Other
1.44k stars 400 forks source link

Ability to delete all unused images. #313

Open paales opened 10 years ago

paales commented 10 years ago

When products and/or categories are deleted the images remain on the webshop.

There is a module that allows you to clean images, but the code quality is very bad and I'm not really for installing it just to fix the images issue. http://www.magentocommerce.com/magento-connect/image-clean.html

fbrnc commented 10 years ago

Some time ago I wrote a simple command line script that traverses all products and all images and hard links found images to media/catalog/product_new. Once this is done you move the current product directory away and move product_new to product. (After creating - or moving - the cache folder inside).

Only problem is loading all products to loop through their images. Unless you get all images by looking at the database tables directly you need to run the different batches in different processes (by splitting them or by forking them - e.g. using Aoe_Threadi). All that makes it probably to specific for something that's bundled with n98-magerun.

paales commented 10 years ago

I believe looking at the database tables directly shouldn't be a problem:

Products:

SELECT * FROM `catalog_product_entity_media_gallery`;

Categories:

SELECT `value` FROM `catalog_category_entity_varchar` ccev
JOIN `eav_attribute` ea ON ccev.`attribute_id` = ea.`attribute_id`
JOIN `eav_entity_type` eet ON ea.`entity_type_id` = eet.`entity_type_id`
WHERE eet.`entity_type_code` = 'catalog_category' AND ea.`attribute_code` IN('image', 'thumbnail')

Secondly traversing through all the directories shouldn't be a problem as well.

joeyhoer commented 10 years ago

@paales That works, but be weary of running that on a store that has custom category image attributes; you'll end up removing a lot more than you bargained for.

For example, if you (or a module/extension) add a custom category attribute - lets say special_image, you would need to amend that query to include special_image in the IN clause.

I've written a query that uses regular expressions to grab the image URLs, it is slightly less accurate in terms of the database relationships, but possibly safer in terms of results.

SELECT * FROM catalog_category_entity_varchar WHERE value REGEXP '.(jpe?g|png|gif|tiff?)$')

Even with this query, you would need to be vigilant about new/less common image types. Think webp, apng, svg. I'm not even sure how many of these image types Magento supports.

pallas commented 10 years ago

@joeyhoer, I think you meant @paales. I have no idea what magerun is.

peterjaap commented 10 years ago

I have a PHP script for this, which I am planning to port to a magerun addon. For now; https://gist.github.com/peterjaap/5547654

brandontamm commented 7 years ago

@peterjaap - Any plans for creating a pull request with your script?

ktomk commented 7 years ago

@brandontamm: Please see this recent comment: https://github.com/netz98/n98-magerun/issues/932#issuecomment-317807644

sprankhub commented 7 years ago

There is already a plugin for that - see https://github.com/magento-hackathon/EAVCleaner / eav:media:remove-unused.