By default, Magento does not report or delete unused product images. With unused product images, I mean images that you can find in the directory "media/catalog/product" and not assigned to any existing product.
Here is what you have to do to automatically delete all those images
- Install the Image Clean Extension
- Create an external file called imgClean.php with the content - Listing 1
- Update class Mage_Imaclean_Model_Mysql4_Imaclean_Collection : add a new method called getImagcleanIds with the content - Listing 2
- Automate the deletion of unused images using automated tasks (cron jobs)
Listing 1
<?php require_once 'app/Mage.php'; umask ( 0 ); $currentStore = Mage::app ()->getStore ()->getId (); Mage::app ()->setCurrentStore ( Mage_Core_Model_App::ADMIN_STORE_ID ); Mage::helper('imaclean')->compareList(); $imacleanIds = Mage::getModel('imaclean/imaclean')->getCollection()->getImagcleanIds(); $model = Mage::getModel('imaclean/imaclean'); foreach ($imacleanIds as $imacleanId) { $model->load($imacleanId); unlink(Mage::getBaseDir ( 'media' ).DS.'catalog/product'. $model->getFilename()); $model->setId($imacleanId)->delete(); } Mage::app ()->setCurrentStore ($currentStore);
Listing 2
<?php public function getImagcleanIds() { $array=array(); try { $coreResource = Mage::getSingleton('core/resource'); $write = $coreResource->getConnection('core_write'); $imacleanTable = $coreResource->getTableName('imaclean'); $sql = "SELECT * FROM " . $imacleanTable; $read = $write->query($sql); while ($row = $read->fetch()) { $array[] = $row['imaclean_id']; } }catch(Exception $e){ Mage::log($e->getMessage()); } return $array; }