Monday, October 19, 2015

Magento: Product Bulk Images Deletion

<?php
require_once('../app/Mage.php');
umask(0);
Mage::app('admin');
ini_set('display_errors', 1);
echo 'Delete Imgaes script runing at '.date("Y-m-d h:i:sa").' <br/>';
try{
    $csv = new Varien_File_Csv();
    $csvfilepath = '../../../../tmp/deleteimages.csv';
   
    $cssfile = fopen($csvfilepath,"r");  
    $data = $csv->getData($csvfilepath);
    //Mage::log('CSV file exsist', null, 'bulkimagesimport.log');
    echo 'CSV file exsist <br/>';
    $rows = count($data);
    for ($j = 1; $j<= $rows; $j++) {
        $productSKU = $data[$j][0];
        if(!empty($productSKU)){
            $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
            if (!$product){
                //Mage::log($productSKU.' SKU doesn`t exsist', null, 'bulkimagesimport.log');
                echo $productSKU.' SKU doesn`t exsist <br/>';
            }else{
                //deleting
                Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);              
                $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
                $product = Mage::getModel('catalog/product')->load($product->getId());
                $items = $mediaApi->items($product->getId());
                $attributes = $product->getTypeInstance()->getSetAttributes();
                $gallery = $attributes['media_gallery'];
                foreach($items as $item){
                    if ($gallery->getBackend()->getImage($product, $item['file'])) {
                        $gallery->getBackend()->removeImage($product, $item['file']);
                    }
                }
                $product->save();
                echo 'Images deleted for sku '.$productSKU.'<br/>';
            }
        }
    }
    fclose($cssfile);
    echo 'Deletion Successfully at '.date("Y-m-d h:i:sa").' <br/>';
}  catch (Exception $e){
    //Mage::log($e->getMessage(), null, 'bulkimagesimport.log');
    echo $e->getMessage();
}  
       

3 comments:

  1. Thanks for sharing and it helped me to delete product images in bulk, but i was also looking for product attribute set in magento, i found it here. How To Configure Product Attribute Sets In Magento

    ReplyDelete
  2. Thanks for sharing tips to delete bult images. Magento Company UK

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete