Sean
Sean

Reputation: 1108

Magento Admin Tool to remove ALL product images

Is there an admin tool in Magento which will remove all images from all products? I know you can go product-by-product and remove all images, but I'm wondering if there's an admin tool which will do all products at once?

Thanks in advance.

Upvotes: 4

Views: 9811

Answers (4)

medina
medina

Reputation: 8159

That's how I fixed it:

    public function __construct(
        \Magento\Catalog\Model\Product $product,
        \Magento\Framework\App\ResourceConnection $resource,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Catalog\Model\Product\Gallery\Processor $processor
    ) {
        $this->productRepository = $productRepository;
        $this->product = $product;
        $this->resource = $resource;
        $this->processor = $processor;
    }

    protected function removeImageGallery($product)
    {
        try {
            $gallery = $this->resource->getTableName('catalog_product_entity_media_gallery');
            $galleryValue = $this->resource->getTableName('catalog_product_entity_media_gallery_value');

            $sql = <<<EOT
DELETE FROM {$gallery}
WHERE value_id IN (SELECT value_id FROM {$galleryValue} WHERE entity_id = {$product->getId()})
EOT;

            $response = $this->resource->getConnection()->query($sql);

            echo '(' . $product->getTypeId() . '): ' . $product->getSku() . " - No. Images: " . $response->rowCount() .  "\n";


//            // Or if you want to try the Magento way
//
//            $images = $product->getMediaGalleryImages();
//            foreach($images as $child){
//                echo '(' . $product->getTypeId() . '): ' . $product->getSku() . ' - ' . $child->getFile() . "\n";
//                $this->processor->removeImage($product, $child->getFile());
//            }
//
//            $this->productRepository->save($product);

        } catch (\Exception $e) {
            echo $e->getMessage() . "\n";
        }
    }

Upvotes: 0

Paul Allsopp
Paul Allsopp

Reputation: 741

It is a bad idea to do this via the DB, but if you must:

ALTER TABLE `catalog_product_entity_media_gallery_value` 
 DROP FOREIGN KEY `FK_CAT_PRD_ENTT_MDA_GLR_VAL_STORE_ID_CORE_STORE_STORE_ID`,
 DROP FOREIGN KEY `FK_CAT_PRD_ENTT_MDA_GLR_VAL_VAL_ID_CAT_PRD_ENTT_MDA_GLR_VAL_ID`;

TRUNCATE `catalog_product_entity_media_gallery_value`;
TRUNCATE `catalog_product_entity_media_gallery`;

ALTER TABLE `catalog_product_entity_media_gallery_value` 
 ADD CONSTRAINT `FK_CAT_PRD_ENTT_MDA_GLR_VAL_STORE_ID_CORE_STORE_STORE_ID` FOREIGN KEY (`store_id`) REFERENCES `core_store` (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE,
 ADD CONSTRAINT `FK_CAT_PRD_ENTT_MDA_GLR_VAL_VAL_ID_CAT_PRD_ENTT_MDA_GLR_VAL_ID` FOREIGN KEY (`value_id`) REFERENCES `catalog_product_entity_media_gallery` (`value_id`) ON DELETE CASCADE ON UPDATE CASCADE;

Then you can delete the product folders with:

cd media/catalog/product
rm -rf *

Upvotes: 2

MXWest
MXWest

Reputation: 401

This method is tested and does work. One reason you might want to do this is when you're testing Dataflow Import for products. When you specify images in the upload, Magento only adds images - it doesn't replace or remove them.

The net result is that several runs of a profile will accumulate a number of images that are superfluous.

mysql> **truncate catalog_product_entity_media_gallery;**
mysql> **truncate catalog_product_entity_media_gallery_value;**

Then, from the command prompt of your Magento media/catalog folder:

media/catalog$ **rm -rf ./product/**

Upvotes: 3

Gershon Herczeg
Gershon Herczeg

Reputation: 3054

I am not sure why you would like to do this but here is a way to do it directly from the Database.

  1. Backup then truncate these 2 tables:

    catalog_product_entity_media_gallery catalog_product_entity_media_gallery_value

  2. then delete '/media/catalog/product'

  3. clear all caches.

I haven't tested it but it should do the job. If it doesnt work then restore those 2 tables

Upvotes: 14

Related Questions