2

I need to remove all product images (small, thumbnail and base) from directory. What would be a better way to do this?

  • 1
    Are you talking about unassigning product images from products? – Vivek Kumar Dec 13 '17 at 13:14
  • Yes! I also want to delete the images from folders (media/catalog/product). All of them. – Andressa Rodrigues Dec 13 '17 at 13:19
3

You can use below code to remove image from product and also from image path

$product = Mage::getModel('catalog/product')->loadByAttribute('sku','acj005');
$MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
$MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product';

$MediaGallery=Mage::getModel('catalog/product_attribute_media_api')->items($product->getId());

foreach($MediaGallery as $eachImge){
    $MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
    $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product';
    $DirImagePath=str_replace("/",DS,$eachImge['file']);
    $DirImagePath=$DirImagePath;
    // remove file from Dir

    $io     = new Varien_Io_File();
    $io->rm($MediaCatalogDir.$DirImagePath);

    $remove=Mage::getModel('catalog/product_attribute_media_api')->remove($product->getId(),$eachImge['file']);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.