Magento Delete all Product Images

The question:

I have a magento Installation which has had several image imports done by the client and its got wrong images in the wrong products, toasters with fridge freezers as the initial import had the wrong img names. After a further import via CSV it seems to have appended the images rather than change them.

I need to delete all product images on the site, then re-do the CORRECT import so all the images are correct.

Is there a quick way to delete all images then do a fresh import via Dataflow Profiles and re-map the correct fields?

Any help would be awesome!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Fastest way to remove image,then follow below steps:
delete all records from

  1. catalog_product_entity_media_gallery
  2. catalog_product_entity_media_gallery_value'

table because magento is save all product image data at those table.

Then index from Index management from admin for set image black.

Then remove image from dir then goto Your magento dir at media/catalog/product and from this folder delete all file.

Another Process:

Andy Simpson,you need a script which is delete all product from your system which will delete from DB and file system.

Step1: Create a php at root direct of magento system which include Mage.php at first code.

require_once "YOURMAGENTODIR/app/Mage.php";
umask(0);

Step2: set current store is admin and set Developer mode

Mage::app('admin');
Mage::setIsDeveloperMode(true);

Step3: Get Product Collection and create a loop for get one product by one by one

$productCollection=Mage::getResourceModel('catalog/product_collection');

Step4: fetch product image by one and remove image one using below code:

$remove=Mage::getModel('catalog/product_attribute_media_api')->remove($product->getId(),$eachImge['file']);

FULL CODE:

<?php
require_once "YOURMAGENTODIR/app/Mage.php";
umask(0);
Mage::app('admin');
Mage::setIsDeveloperMode(true);

$productCollection=Mage::getResourceModel('catalog/product_collection');
foreach($productCollection as $product){
echo $product->getId();
echo "<br/>";
         $MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
        echo $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product';
echo "<br/>";

$MediaGallery=Mage::getModel('catalog/product_attribute_media_api')->items($product->getId());
echo "<pre>";
print_r($MediaGallery);
echo "</pre>";

    foreach($MediaGallery as $eachImge){
        $MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
        $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product';
        $DirImagePath=str_replace("/",DS,$eachImge['file']);
        $DirImagePath=$DirImagePath;
        // remove file from Dir

        $io     = new Varien_Io_File();
         $io->rm($MediaCatalogDir.$DirImagePath);

        $remove=Mage::getModel('catalog/product_attribute_media_api')->remove($product->getId(),$eachImge['file']);
    }


}

Method 2

The full mysql statement for Amit’s answer is

SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE catalog_product_entity_media_gallery;
TRUNCATE TABLE catalog_product_entity_media_gallery_value;
SET FOREIGN_KEY_CHECKS = 1;

Method 3

I don’t think there is a quick fix to this unless you can reimport all your products from a backup export with the correct image data.

You could use a PHP script to loop through all your products and remove the media. Have a look at https://stackoverflow.com/questions/5709496/magento-programmatically-remove-product-images for more info.

You can safely delete /media/catalog/product/ and restore if you have a working backup with the correct image files.

Method 4

I used to Kevando on magento 2.2.7 but when import images again will not exactly. So this is my answers it’s be tested.

SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE catalog_product_entity_media_gallery;
TRUNCATE TABLE catalog_product_entity_media_gallery_value;
TRUNCATE TABLE catalog_product_entity_media_gallery_value_to_entity;
SET FOREIGN_KEY_CHECKS = 1;

Method 5

Thanks “Amit Bera”, It’s saved my day and working fine

Just I followed the below procedure to delete all product images.

Fastest way to remove image,then follow below steps: delete all records from

catalog_product_entity_media_gallery
catalog_product_entity_media_gallery_value’
table because magento is save all product image data at those table.

Then index from Index management from admin for set image black.

Then remove image from dir then goto Your magento dir at media/catalog/product and from this folder delete all file.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment