Removing unused product images in magento

The image-clean module lists unused images in the / media / catalog / product directory and allows you to delete them. Is there a script that automatically deletes unused images without user intervention? I want to run this script manually or use cron work every night.

thanks

+6
image product magento
source share
2 answers

If you look at the source for this module controller controller, you can see the code that they use to perform bulk removal

#File: app/code/local/Mage/Imaclean/controllers/Adminhtml/ImacleanController.php public function massDeleteAction() { $imacleanIds = $this->getRequest()->getParam('imaclean'); if(!is_array($imacleanIds)) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)')); } else { try { $model = Mage::getModel('imaclean/imaclean'); foreach ($imacleanIds as $imacleanId) { $model->load($imacleanId); unlink('media/catalog/product'. $model->getFilename()); $model->setId($imacleanId)->delete(); } Mage::getSingleton('adminhtml/session')->addSuccess( Mage::helper('adminhtml')->__( 'Total of %d record(s) were successfully deleted', count($imacleanIds) ) ); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } } $this->_redirect('*/*/index'); } 

So, this controller action accepts many identifiers of the imaclean / imaclean model, uses these identifiers to perform the deletion. So, the key code in this action

 $imacleanIds = $this->getRequest()->getParam('imaclean'); $model = Mage::getModel('imaclean/imaclean'); foreach ($imacleanIds as $imacleanId) { $model->load($imacleanId); unlink('media/catalog/product'. $model->getFilename()); $model->setId($imacleanId)->delete(); } 

So you can reproduce the above code in standalone version with something like

 //itterates through all 'imaclean/imaclean' models in the database $models = Mage::getModel('imaclean/imaclean')->getCollection(); foreach ($models as $model) { unlink('media/catalog/product'. $model->getFilename()); $model->setId($model->getId())->delete(); } 

Finally, it appears that the imaclean / imaclean models are used to track which images are no longer needed. It looks like the module creates these (i.e., launches validation for unused images) in newAction using the compareList helper compareList method.

 public function newAction(){ Mage::helper('imaclean')->compareList(); $this->_redirect('*/*/'); } 

So, we can add this to the beginning of our script, as well as the de facto initialization of Magento, which should give us what we need.

 #File: cleanup.php require_once "app/Mage.php"; $app = Mage::app("default"); Mage::helper('imaclean')->compareList(); $models = Mage::getModel('imaclean/imaclean')->getCollection(); foreach ($models as $model) { unlink('media/catalog/product'. $model->getFilename()); $model->setId($model->getId())->delete(); } 

This should at least begin. Good luck

+10
source share

There is a script here when deleting multimedia images, be sure to back up the database and media before starting work. There is also an SQL statement that deletes gallery entries that no longer have an assigned product.

http://www.codefuel.co.uk/magento-removing-media-that-doesnt-belong-to-products/ I used this on magento version 1.8.x and it works fine.

+1
source share

All Articles