Need to remove all product images from your Magento site? Want to do a site-wide product image resize to speed up your size?
Below are some ways we can accomplish this.
Remove the image product association from database
This is probably the fastest way to remove all product images for a Magento site, just follow below steps:
Truncate (delete all records from) the two tables in your db: catalog_product_entity_media_gallery, catalog_product_entity_media_gallery_value
Magento saves all product image data at those tables. catalog_product_entity_media_gallery table stores image path info,
and catalog_product_entity_media_gallery_value table holds the image order and if the image is disable.
Just log into phpMyAdmin or use MySQL console to run the SQL statement below.
1 2 |
TRUNCATE TABLE catalog_product_entity_media_gallery; TRUNCATE TABLE catalog_product_entity_media_gallery_value; |
Then log into Magento backend, reindex and it;s done. However this method will not delete the initial images you uploaded before. These images file will remain in media/catalog/product. You can manually delete them or use the script below to delete the image from db and file system at the same time.
Remove images and delete image files
You can run a php script, which uses Magento APIs to remove assigned images for all products, and also delete the image files on the server.
Setp 1. Create a PHP file at your Magento installation root, name it delete_image.php
Step 2. Copy and paste the code below to the PHP file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php require_once "app/Mage.php"; umask(0); // set current store to admin and turn on developer mode Mage::app('admin'); Mage::setIsDeveloperMode(true); // load product collection $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 server $io = new Varien_Io_File(); $io->rm($MediaCatalogDir.$DirImagePath); // remove product image association in db $remove=Mage::getModel('catalog/product_attribute_media_api')->remove($product->getId(),$eachImge['file']); } } |
Step 3. run and execute the PHP script at www.yourmagentodomain.com/delete_image.php
This may take a bit while if you have a large catalog due to I/O tasks involved.
Also, Remember to flush the catalog image cache. System > Cache Management > Flush Catalog Images Cach