diff --git a/vendor/magento/module-catalog/Model/Product/Gallery/CreateHandler.php b/vendor/magento/module-catalog/Model/Product/Gallery/CreateHandler.php index a5572e787250e..4ac0ec823d2cf 100644 --- a/vendor/magento/module-catalog/Model/Product/Gallery/CreateHandler.php +++ b/vendor/magento/module-catalog/Model/Product/Gallery/CreateHandler.php @@ -189,9 +189,18 @@ public function execute($product, $arguments = []) $clearImages[] = $image['file']; } elseif (empty($image['value_id']) || !empty($image['recreate'])) { $newFile = $this->moveImageFromTmp($image['file'] ?? ''); - $image['new_file'] = $newFile; - $newImages[$image['file']] = $image; - $image['file'] = $newFile; + if (!empty($image['recreate']) && $newFile !== $image['file']) { + //delete old image + $this->mediaDirectory->renameFile( + $this->mediaConfig->getMediaPath($newFile), + $this->mediaConfig->getMediaPath($image['file']) + ); + $existImages[$image['file']] = $image; + } else { + $image['new_file'] = $newFile; + $newImages[$image['file']] = $image; + $image['file'] = $newFile; + } } else { $existImages[$image['file']] = $image; }