Magento2: Remove all product Image

<?php

use Magento\Framework\App\Bootstrap;
use Magento\Sales\Model\Order;
use Magento\Catalog\Model\Product as ModelProduct;

require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$obj->get('\Magento\Framework\App\State')->setAreaCode('frontend'); // for remove Area code is not set error
$storeManager = $obj->get('\Magento\Store\Model\StoreManagerInterface');
$baseUrl = $storeManager->getStore()->getBaseUrl();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
/** Apply filters here */
$collection = $productCollection->addAttributeToSelect('*')
              ->addAttributeToFilter('type_id', 'configurable')
                   ->load();

foreach ($collection as $value) {  
      // Instance of object manager
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $productId = $value->getId(); // Id of product
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
    $productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
    $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
    foreach ($existingMediaGalleryEntries as $key => $entry) {
        unset($existingMediaGalleryEntries[$key]);
    }
    $product->setMediaGalleryEntries($existingMediaGalleryEntries);
    $productRepository->save($product);
    $product->save();

Comments