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, 2014 at 10:35

1 Answer 1


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

require_once 'app/Mage.php';

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

$products = Mage::getModel('catalog/product')
                ->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, 2014 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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