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

  • How you can edit product from Front End? – Ankit Shah Oct 6 '16 at 7:31
  • We are creating a panel on frontend where some user can create/edit/delete the products. But during editing we are facing problem in removing the images of product. – Ranjeet Singh Oct 6 '16 at 7:36
  • Please update the question & if possible put the code & error so can help out – Ankit Shah Oct 6 '16 at 7:39
  • We are not getting any error, we just want to discover that what code we need to implement to remove image of a particular product – Ranjeet Singh Oct 6 '16 at 9:24
  • How about your issue? – Khoa TruongDinh Oct 18 '16 at 0:07

We can use \Magento\Catalog\Model\ProductFactory to get media gallery. We also need to declare the \Magento\Catalog\Api\ProductRepositoryInterface class which helps us to save the existing product.

We can use Object Manager directly, however, this way isn't good. It's better that we should inject these class in the constructor.

/**@var \Magento\Catalog\Model\ProductFactory **/
protected $product;

/**@var \Magento\Catalog\Api\ProductRepositoryInterface **/
protected $productRepository;

public function __construct(
   \Magento\Catalog\Model\ProductFactory $product,
   \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  $this->product = $product;
  $this->productRepository = $productRepository;

In your custom method:

//Product id
$productId = 12;
$product = $this->product->create();

$existingMediaGalleryEntries = $product->load($productId)->getMediaGalleryEntries();

foreach ($existingMediaGalleryEntries as $key => $entry) {
    //We can add your condition here

Should take a look at the Unit test:


  • using this code when i add other base image programmatically that image is not set as base image so how to remove all base,thumbnail,small image ? – ND17 Nov 24 '16 at 10:47
  • @ND17 you want to remove all base, thumbnail, small images? My code above cannot help you anymore? – Khoa TruongDinh Nov 24 '16 at 10:50
  • your code is remove all images but when i try to save other image as base image, it is not set as base image and if i remove all images and save same image which i am removing than that image is set as base image – ND17 Nov 24 '16 at 10:53
  • this is happens beacuse cache folder is exist in media\catalog\product – ND17 Nov 24 '16 at 11:20
  • @KhoaTruongDinh i tried above solution but it fires constraint exception? – Sujeet Pandit Mar 22 '18 at 7:54

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

  • 2
    You shouldn't use ObjectManager... apart from that, your code is good. – leedch Jan 4 '18 at 14:50
  • this gives me integrity constraint violations – TTech IT Solutions Sep 15 '18 at 19:43
  • Not working for disable images – Nimesh Patel Nov 21 at 10:53

I was working on similar task, I am sharing my code, hope it would help others too.

Fist initialization in constructor

protected $productModel;
protected $imageProcessor;

public function __construct(
    \Magento\Catalog\Model\Product $productModel,
    \Magento\Catalog\Model\Product\Gallery\Processor $imageProcessor,
    $this->productModel = $productModel;
    $this->imageProcessor = $imageProcessor;

Now you can use it in your function

$product = $this->productModel->load($productId);
$gallery = $product->getMediaGalleryImages();
foreach($gallery as $image){

This should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.