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
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.
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With