I'm in the process of replacing my product images with new photos with the same filename. When I delete the old image in magento then reupload the new image, it adds _1 to the end of the filename, which makes me think it hasnt really deleted the old image. How can I fully delete the old image via magento so when I re-upload it keeps the correct filename?
Magento – Replace product image without getting _1 in filename
product-images
Related Solutions
This was already taken care of with a module. Image Clean works with 1.4.2.0 on up to 1.7 and is available over on Magento Connect as a free install.
I recommend it as your issue will be an ongoing one. Magento does not remove images when product are deleted so not only do you have this import issue to contend with every time something goes akilter, but you have an ongoing and never ending disk usage bloat as Magento's less than stellar housekeeping lets it pack on the poundage in unused images from long dead and removed product.
Note: no affiliation with the above mentioned module. However, having tried to go the scripting route to manage images, found that this developer produced a module that works simply and efficiently. Just don't let it delete your placeholder images for "No image found"
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.
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']);
}
}
Best Answer
I haven't tested this myself but you could try looking at:
Mage_Adminhtml_Catalog_Product_GalleryController::uploadAction()
In particular the line
$uploader->setAllowRenameFiles(true);
The PHPDoc says:
Try setting it to false and see if images are still appended with a number.
If that doesn't work, take a look at
Mage_Catalog_Model_Resource_Product_Attribute_Backend_Image::afterSave()
and try the same as above.It would be great if you could post your results so other people can know which method works.