2

Try to Remove Products Images but got error in magento2

Error : "Product with SKU '12553' is not linked to product with SKU '12552'" in

My code is:

$get_ids = array(12551,12552,12553,12554);
foreach($get_ids as $ids){
 $product = $objectManager->get('Magento\Catalog\Model\Product')->load($ids);
 $productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
 $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
 foreach ($existingMediaGalleryEntries as $key => $entry) {
 unset($existingMediaGalleryEntries[$key]);
 }
 $product->setMediaGalleryEntries($existingMediaGalleryEntries);
 $productRepository->save($product);
}
asked Jul 11, 2017 at 16:59
1
  • Please confirm whether the products belongs to simple ? Commented Jul 13, 2017 at 11:29

3 Answers 3

6
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$productArray = array("2011", "2012", "2013");
//Instance of object manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
foreach($productArray as $key=>$productId)
{ 
$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);
echo "Product Image has been deleted for".$productId."</br>";
}
echo "Yahooooo";
?>
answered Jul 13, 2017 at 11:30
0

i want to remove through sku but i could not be delete i just want to first read the file then remove product images accordingly csv file skus anyone can help me for this solution

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 0); 
set_time_limit(0);
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
//Instance of object manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
//skus
// $productArray = array("7750087","7750100");
$file = fopen('skus.csv', 'r', '"'); // set path to the CSV file
$header = fgetcsv($file); // get data headers and skip 1st row
while ( $row = fgetcsv($file, 40000, ",") ) {
 $data = array();
 $data = array_combine($header, $row);
 $productSkus=$data["sku"];
 foreach($productArray as $key=>$productSkus){
 $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);
echo "Product Image has been deleted for".$productSkus."</br>";
}
}
echo "Yahooooo";
 ?>
answered Jun 21, 2021 at 10:54
0

The other solutions did not work for me. This one did, tested on M2.4.3.

private $gallery_rm
 
public function __construct(
 \Magento\Catalog\Model\ResourceModel\Product\Gallery $gallery_rm
) {
 $this->gallery_rm = $gallery_rm;
}
public function removeImagesFromProduct($product_object) {
 // get all images from product object
 $media_gallery_entries = $product_object->getMediaGalleryEntries();
 foreach($media_gallery_entries as $product_img) {
 $this->gallery_rm->deleteGallery($product_img->getId());
 }
}
answered Mar 30, 2023 at 20:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.