Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top