Delete unused product images in magento

13,260

Solution 1

If you take a look at the source for that module's admin controller, you can see the code they use to perform a mass delete

#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 a number of "imaclean/imaclean" model ids, uses these ids to perform a delete. So, the key code in that action is

$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 could replicated the above code in a stand-alone 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 looks like an "imaclean/imaclean" models are used to keep track which images are no longer needed. It looks like the module creates these (i.e. Runs a check for unused images), in the newAction with the compareList method of the default helper.

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

So, we can add that to the start of our script, as well as the de-facto Magento initialization, 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();
}   

That should at least get you started. Good luck!

Solution 2

There is a script here on removing media images, be sure to backup database and media before hand. Also there is a SQL statement on there which removes gallery records which don't have any product assigned to anymore.

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

Share:
13,260
pablo
Author by

pablo

Updated on June 28, 2022

Comments

  • pablo
    pablo about 2 years

    The image-clean module lists unused images under /media/catalog/product and let you delete them. Is there a script that automatically delete unused images without user interaction? I want to run this script manually or use a cron job every night.

    Thanks

  • pablo
    pablo over 13 years
    I don't want to use a module for that because too many modules confuses the admin. I also don't need another db table just to delete old images. I'll try to take only the code I need from the module and post the result. Thanks
  • styks
    styks over 11 years
    Did you ever come up with a solution?
  • MageDev
    MageDev over 10 years
    @Alan Storm if we use above cleanup.php. is it affect our other image? or only delete those images which is exist but its product deleted?
  • Flyingmana
    Flyingmana over 9 years
    be carefull, this code has some potential security risks, but still usefull