I am writing an import product job on Magento 2. I want to delete all product images if I need to update the product. I try to use $product->getMediaGalleryImages()
to get all the images from the product but it returns null after running. I ensure that the product has an image. Follows is the code I tried.
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product = $product->getCollection()->addFieldToSelect("*")->addAttributeToFilter('sku', 'sku-01')->getFirstItem();
$images = $product->getMediaGalleryImages(); // it return null
And follows is the code to upload product image.
$product->setMediaGallery(array('images' => array(), 'values' => array()))
->addImageToMediaGallery($image_directory, array('image', 'thumbnail', 'small_image'), false, false);