3

How can I remove not existing images from magento. In the database have images but file is not exist. I want deleted this images from database.

3 Answers 3

2

Create a test script mediaDelete.php at magento root and add below code and run that file from Command line

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;

require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');

$resourceConnection = $objectManager->create(\Magento\Framework\App\ResourceConnection::class);
$mediaConfig = $objectManager->get(\Magento\Catalog\Model\Product\Media\Config::class);
$filesystem = $objectManager->create(\Magento\Framework\Filesystem::class);
$mediaDir = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);

$connection = $resourceConnection->getConnection('core_write');
$tableName = $resourceConnection->getTableName('catalog_product_entity_media_gallery');
$result = $connection->fetchAll('SELECT * FROM .$tableName. where media_type = "image"');
foreach ($result as $item) {
    $imagePath = $mediaDir->getAbsolutePath($item['value']);

    if (!$mediaDir->isExist($imagePath)) {
        $sql = "Delete FROM " . $tableName . " Where value_id = " . $item['value_id'];
        $connection->query($sql);
    }
}

Note, Code is not tested and the code shared base on the idea.

3
  • Thanks for help, but it can remove all image because path should be ../pub/media/a/b/ab.jpeg but actually product image path is ../pub/media/catalog/product/a/b/ab.jpeg so just change image path it work Commented Jun 4, 2019 at 7:03
  • OPPS, it was a mistake on if (!$mediaDir->isExist($imagePath)) { and it fixed now.
    – Amit Bera
    Commented Jun 4, 2019 at 7:36
  • Had to adjust to $imagePath = $mediaDir->getAbsolutePath('catalog/product'.$item['value']); apart from that... perfect
    – elfling
    Commented Feb 2 at 16:27
3

If you are looking for a direct solution for your store then you can use any of these opensource modules for your task,

https://github.com/sivaschenko/magento2-clean-media

https://cannycookie.github.io/Mage2UnusedImageRemover/

https://github.com/magento-hackathon/EAVCleaner

1

You can create a controller for deleting non-exist product images using following code, please have a look:

protected $_productCollection;
protected $_product;
protected $_driveFile;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,
    \Magento\Catalog\Model\Product $product,
    \Magento\Framework\Filesystem\Driver\File $driveFile
) {
    $this->_productCollection = $productCollection;
    $this->_product = $product;
    $this->_driveFile = $driveFile;
    parent::__construct($context);
}

public function execute() {
    // Get Product Ids list, you can change filter and condition accordingly
    $productIds = $this->_productCollection->create()->getAllIds();

    foreach ($productIds as $id) {
        // Load product by Id, because you can get media gallery images only using Product Model
        $productObj = $this->_product->load($id);

        // Get product media gallery images
        $images = $productObj->getMediaGalleryImages()->getItems();

        foreach ($images as $image) {
            $imagePath = $image->getPath();

            // Check if image exist, if not, then delete it
            if (!$this->_driverFile->isExists($imagePath))  {
                $this->_driverFile->deleteFile($imagePath);
            }
        }
    }

I hope this may helpful to you!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.