2

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.

1

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.

| improve this answer | |
  • 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 – Chandresh Chauhan Jun 4 '19 at 7:03
  • OPPS, it was a mistake on if (!$mediaDir->isExist($imagePath)) { and it fixed now. – Amit Bera Jun 4 '19 at 7:36
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

| improve this answer | |
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!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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