We had several issues when trying to build a catalog for a customer using native Import/Export functionality. Unfortunately it seems Magento2 has some difficult to download & process cloudfront.net images, so we wrote our own command to manage that

Process is working fine, problem here is we need to call about 10 times addImageToMediaGallery() for each product. That calls are processed in a reasonable time, but then, at the end when calling the product save... it takes a long time

We have other processes to update product information (text attributes, stock data, etc...) in this project, and they all run fast, so it seems clear there is something about image processing which kills the save process performance

Our relevant code would be this

foreach ($parseData as $row) {

    // I omit the part where we get $sku from $row...
    // $productRepository -> Magento\Catalog\Model\ProductRepository
    $product = $productRepository->get($sku);

    // Delete existing Magento images from DB and filesystem
    $existingMedia = $product->getMediaGalleryEntries();
    if (is_array($existingMedia) && count($existingMedia) > 0) {
        foreach ($existingMedia as $existingMediaItem) {
            // $galleryProcessor -> Magento\Catalog\Model\Product\Gallery\Processor
            $galleryProcessor->removeImage($product, $existingMediaItem['file']);

    // Import new images

    foreach ($row as $i => $remoteImage) {
        // I omit the part where we download, save to filesystem & get $image
        // and the logic to set the right attributes in $media variable
        $product->addImageToMediaGallery($image, $media, false, false);

    // and finally... the kill of performance... 
    // all previous code runs reasonable fine, no need to change anything

We have tried different ways, $product->save() was our first try (same kill of performance)

So, any tips to save the product in a decent time? Is there any way to update image attributes values using something like \Magento\Catalog\Model\ResourceModel\Product\Action::updateAttributes, which works perfect & fast?

This is CE 2.3.0 version, by the way

  • 1
    I am facing the same situation on 2.2.7. I cant find a solution. I'm toying with the idea of direct mysql queries although as solid as I can make them i.e. try { $connection->beginTransaction(); $connection->update($this->getTable('etc'), ['value' => $value], $where); $connection->commit(); } catch(\Exception $e) { $connection->rollBack(); $this->logger->critical($e); } May 16, 2019 at 21:31
  • 1
    Give this a try: magento.stackexchange.com/a/240362/146 I used it for videos. I think you can adapt it for images since they are kept ins the same tables. If you want speed, first thing is to drop completely calls to the save methods. In the answer I linked, I used direct db inserts. A bit risky, but it worked for me and it was really fast.
    – Marius
    May 20, 2019 at 19:33
  • 1
    Is your pub/media is on same device? Best if its on SSD of the same device like a rest. community.magento.com/t5/Magento-2-x-Programming/…
    – BartZalas
    May 21, 2019 at 0:15
  • 1
    Have you tried to change the indexing setting to "update by schedule" ?, do the indexing using cron, not at the time of saving product.
    – Saphal Jha
    May 21, 2019 at 12:03
  • 1
    as I have again read all the description, it's taking time at the time of image download and save it, right?
    – Saphal Jha
    May 21, 2019 at 13:35


Your Answer

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