Magento-1.9 – How to Delete All Images from Products

ce-1.9.0.1magento-1.9product-images

We have a batch of images that belong to products already in the database. For that, we need to delete all images connected to each product being updated, then add the new/changed images to that product.

There's a lot of answers on how to delete all images from a product, but it seems that every single one fails if there's a condition present (even the backend fails):

We have multiple storeviews, and in one of those storeviews ("Default Store View") one of the images to be deleted is listed as the standard image (base, small, thumbnail). As long as this is the case, neither any code nor the backend is able to remove the images from a product while being set to the "Admin" store view.

I'm using this code to remove the images (at least, all except the one in question):

$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$items = $mediaApi->items($product->getId());
foreach($items as $item) {
        echo $mediaApi->remove($product->getId(), $item['file']);

}

Is there a way to reset the store view to use the "Admin" storeview settings (aka setting all 3 image types to "Use standard") by code? I've already tried this:

$values = array(
    'image'=>'no_selection',
    'small_image'=>'no_selection',
    'thumbnail'=>'no_selection',
);
foreach (Mage::app()->getWebsites() as $website) {
    foreach ($website->getGroups() as $group) {
        $stores = $group->getStores();
        foreach ($stores as $store) {
            Mage::app()->setCurrentStore($store);
                Mage::getSingleton('catalog/product_action')->updateAttributes($product->getId(), $values, 0);
        }
    }
}

That didn't seem to change anything.

Best Answer

Try to load the product each time with the different stores id like this:

Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId);

Then you can remove the images.

Related Topic