Batch remove images from products in Magento

After a slightly botched image upload attempt which left duplicate images on certain products, a client wanted to start again on the image-front by removing all the images from the products. So a nice easy way to do this programmatically had to be found – and here it is :

Batch Removing Product Images in Magento


Start off with the usual gubbins :

<?php
require_once 'app/Mage.php';
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

Then load into $products the products that we want to use. In this case we were dealing with a batch of the most recent products uploaded, so we were looking for those products with an ID greater than 14000 :

$products = Mage::getModel('catalog/product')->getCollection()
	->addAttributeToFilter('entity_id', array('gt' => 14000));

The Media API is going to do the hard work for us :

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

So then loop through each product, get its ID, load the product, load its media items, and if it has any, get the ID of each media item associated with it and delete it – that’s all there is to it :

foreach($products as $product) {
	$prodID = $product->getId();
	$_product = Mage::getModel('catalog/product')->load($prodID);
	$items = $mediaApi->items($_product->getId());
	if (count($items) > 0) { 
		foreach($items as $item) {
			$mediaApi->remove($_product->getId(), $item['file']);
		}
		echo $product->getId() . " done \n";
	} else { 
		echo $product->getId() . " has no images \n";
	}
}

And then finish off…

?>

And there you have it. If you have a lot of images to deal with, probably best to call this from a cron job.

30 comments

  • ZG

    Hi Giles,

    I know it’s been quite a while since this post. Been searching for a “non-extension” way to bulk delete images and came across this script. I put the php file together, loaded into Magento root, and then hit the URL, but nothing seems to happen. I am testing it using a specific range of ID’s

    EX:

    $products = Mage::getModel(‘catalog/product’)->getCollection()
    ->addAttributeToFilter(‘entity_id’, array(‘gt’ => 12525));
    ->addAttributeToFilter(‘entity_id’, array(‘lt’ => 12528));

    Again, we are probably talking about 4 versions ago when this post was first created, I am in Mage 1.9.2.4

    Any suggestions would be GREATLY appreciated. Thanks!

    • Giles Bennett (author)

      Hi there – maybe try running it over SSH, from the command line, rather than through the browser. If there’s an error, then the error will be returned to you. You could try adding the following line to the top of your script, if executed through the browser :

      ini_set('display_errors', 1);
      
  • Alex

    Hi Giles, can you suggest a script that would do exact the opposite? I want to bulk assign images to products based on SKUs. I do have a script (if you want i can share it with you) that will do what i need but problem is that will assign only 1 image. I would need the script if the product has sku “example” to look for example.jpg,example_01.jpg etc and assign all the images (choose the 1st as main image) Also my script other problem is that not updating images but only assign images if the product is new sku. Any suggestions? Thanks!

    • Giles (author)

      Alex,

      This script does the converse.

      Kind regards,

      Giles

  • Kim

    Thank you so much for sharing this! It will save me a ton of headache.

  • Hugh

    Hi Giles.

    Could I use this to delete images from a set of products dependent on partial SKU?

    Example:

    I want to replace all images on a configurable product [SKU: DBBC] and all 59 simple products under it [example SKU: DBBC-Blue (601)]

    Therefore, I want to run this on all products with a SKU starting DBBC.

    Is this possible?

    Thanks,

    Hugh

    • Giles (author)

      Hugh,

      Yes – if you change the way in which the product collection is filtered (so have it match a specific pattern of SKU, rather than those products with an ID greater than X) then it will return matching products and remove images from those.

      Kind regards,

      Giles

  • Mazhar

    Hi Giles,

    Which magento folder do i copy this file to and how do i run? I am new to magento and need some guidance.

    Thanks

    • Giles (author)

      Mazhar,

      You would upload the file to your web root, and then run the file by visiting it in a browser.

      Kind regards,

      Giles

  • Henk Valk

    Is it possible to delete only the product images of gallery?

    • Giles (author)

      You mean those images which aren’t set to be base, main or thumbnail images?

      Kind regards,

      Giles

      • Henk Valk

        Indeed the other images

        • Giles (author)

          Probably, but not easily. You’d probably have to load the three images that are in use as the specified images for the product into a separate array, then prior to deletion compare the image being processed with the array, and if it’s in the array, don’t delete it.

          Kind regards,

          Giles

  • Maxime

    Very slow… it take 6 sec for one product… Need a better way !

    • Giles (author)

      More likely that you need a better hosting solution – it doesn’t take anything like that long for us.

      Kind regards,

      Giles

  • Kerim

    Excellent. Very helpful article.

  • Tomatom

    Hi, What does it mean “remove” in “Batch remove…” heading? It means that producst losts their images, so in admin area there are no images in product detail at all, OR it means that it only change association to “no_selection”? So images are still there but they are not used as base, small and thumbnail image?

    • Giles (author)

      It means that the images are removed completely.

      Kind regards,

      Giles

  • Dylan P.

    How do I do this if I’m not a programmer? lol

    • Giles (author)

      Dylan,

      Pay a programmer to do it for you!

      Kind regards,

      Giles

  • Roy

    Fantastic! Thanks for this. Really saved the day!

    • Giles (author)

      Roy,

      You’re more than welcome. Glad it helped.

      Giles

  • Dave

    Stopped me from banging my head on the wall. Thanks so much.

    • Giles (author)

      Dave,

      No problem – glad you found it useful!

      Giles

  • Lionel

    one tiny question. I don’t know PHP enough. how would you go about setting the array to be between 2 pids. say >= 14000 and =< 18000.

    thanks for the code, though!

    • Giles (author)

      Lionel,

      You can add multiple filters to the collection, so rather than :

      $products = Mage::getModel('catalog/product')->getCollection()
      	->addAttributeToFilter('entity_id', array('gt' => 14000));
      

      you could instead have :

      $products = Mage::getModel('catalog/product')->getCollection()
      	->addAttributeToFilter('entity_id', array('gt' => 14000))
      	->addAttributeToFilter('entity_id', array('lt' => 18000));
      

      That should then get you all products with an ID from 14000 to 18000.

      Giles

  • Lionel

    If I was in the same room as you, I’d hug you.

    thank you for saving me days ….

    • Giles (author)

      I’ll consider myself hugged!

      Giles

  • Carlos

    Saved my day. Thankyou!!

    • Giles (author)

      You’re very welcome!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to talk to us about your project?