Magento – Magento2: How to remove product images programmatically

magento2product-images

During edit product from front-end I need to remove images of the product
gallery which are checked by the user to remove it.

Best Answer

Below code working fine for me to remove Image Magento 2.

Using Product ID:

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productNewId);
$imageProcessor = $objectManager->create('\Magento\Catalog\Model\Product\Gallery\Processor');
$images = $product->getMediaGalleryImages();
foreach($images as $child){
    $imageProcessor->removeImage($product, $child->getFile());
}   

Hope it helps to you

Related Topic