0

I recently uploaded some images for products and accidently duplicated them for products that are all listed within a specific category.

I've looked around and found a solution that claims to delete all images, but I want to do this for a specific category (category ID).

I can't be the first to have had this happen.

How can I delete all product images (only the images) from a specific category?

  • Just filter your collection with category id and use solution you have found in blog – Mufaddal Jul 2 '14 at 10:35
2

Using the script you provided as solution, you only need to filter the product collection by category id

<?php
require_once 'app/Mage.php';
Mage::app('admin');

$categoryId = 3; // set your category ID here

$products = Mage::getModel('catalog/product')
                ->getCollection()
                ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                ->addAttributeToFilter('category_id', array('eq' => $categoryId));

$mediaApi = Mage::getModel("catalog/product_attribute_media_api");

foreach(array_keys($products->getItems()) as $productId) {
    $items = $mediaApi->items($productId);
    if (count($items) > 0) { 
        foreach($items as $item) {
            $mediaApi->remove($productId, $item['file']);
        }
        echo $productId . " done \n";
    } else { 
        echo $productId . " has no images \n";
    }
}
  • Beautiful, it works a little slowly but does the job. Thank you. SR – SR_Magento Jul 2 '14 at 17:03

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.