Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete unused product images in magento

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

like image 315
pablo Avatar asked Dec 02 '10 20:12

pablo


People also ask

Where are product images stored in Magento?

All the images are stored under the pub/media/catalog/product folder in the Magento root. If the product image name is abc. jpg then it's stored under the above folder with an a/b/abc.

How do I bulk delete items in Magento?

Bulk Delete You can delete products from Magento 2 in bulk by using the same process as Bulk Update but by unchecking “Web” while updating products. This will delete the products from Magento 2 during the next sync.


2 Answers

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!

like image 153
Alan Storm Avatar answered Nov 15 '22 05:11

Alan Storm


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.

like image 41
Rees McIvor Avatar answered Nov 15 '22 05:11

Rees McIvor