How to delete images from any products programmatically in magento 2

If you want to delete images from any products programmatically in magento 2 you can use this code to acheive the required results.

 


$productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
$product_data1 = $productRepository->get($data['sku']);
$productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
$existingMediaGalleryEntries = $product_data1->getMediaGalleryEntries();
foreach ($existingMediaGalleryEntries as $key => $entry) {
unset($existingMediaGalleryEntries[$key]);
}
$product_data1->setMediaGalleryEntries($existingMediaGalleryEntries);
$productRepository->save($product_data1);

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top