Skip to content

Instantly share code, notes, and snippets.

@miguelbalparda
Created June 4, 2015 13:23
Show Gist options
  • Save miguelbalparda/7fa2453be35f9ed8f56d to your computer and use it in GitHub Desktop.
Save miguelbalparda/7fa2453be35f9ed8f56d to your computer and use it in GitHub Desktop.
<?php
/*
If your products have been deleted but not your media gallery images you
can use the following SQL statement to remove these dormant records.
Please make sure you backup your installation and database before running this
SQL statement. It has been used on Mage 1.8.1 system:
DELETE `img` FROM `catalog_product_entity_media_gallery` img
LEFT JOIN `catalog_product_entity` AS prod ON img.entity_id = prod.entity_id
WHERE prod.sku IS NULL
*/
try {
$sMageFile = __DIR__ . '/app/Mage.php';
if(!is_file($sMageFile)) {
throw new Exception('Cannot find: ' . $sMageFile);
}
require_once($sMageFile);
$oApp = Mage::app();
// Set area to admin
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$sImgDir = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product';
$oResource = Mage::getSingleton('core/resource');
$sMediaTbl = $oResource->getTableName('catalog_product_entity_media_gallery');
$oReadConn = $oResource->getConnection('core_read');
$i=0;
$oIterator = new RecursiveDirectoryIterator($sImgDir);
foreach( new RecursiveIteratorIterator($oIterator) as $sFile) {
if(strpos($sFile, "/cache") !== false || is_dir($sFile) ) {
continue;
}
$sFilePath = str_replace($sImgDir, "", $sFile);
$sQuery =
'SELECT value FROM ' . $sMediaTbl . ' WHERE value="' . $sFilePath . '"';
$sValue = $oReadConn->fetchOne($sQuery);
if($sValue == false){
echo "## REMOVING: " . $sFilePath . " ## \n";
unlink($sFile);
$i++;
}
}
echo "\r\n\r\n Finished removing $i un-used product images\r\n\r\n";
} catch (Exception $e) {
echo '<pre style="color:red;">' . $e->getMessage() . '</pre>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment