Q:

How do you remove all images in magento?

Ive tried to look everywhere online for a solution to delete all of the product images assigned to products in magento. Obviously when I add my clients new images on import, magento just appends the images which is not what we are looking for. We want to wipe all products clean of images and replace with a fresh import. Ive tried a few scripts and other methods which havent worked for my version of magento ver. 1.9.0.1

A:

 //deleting
 Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
 $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
 $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();

