Magento2: The image cannot be removed as it has been assigned to the other image role

Created on 1 Dec 2016  Â·  38Comments  Â·  Source: magento/magento2

Preconditions

Magento 2.1.2, PHP 7.0.13, MySQL 5.7.16

Steps to reproduce

  1. Edit a Product
  2. Delete an image not assigned to a role

Expected result

  1. Image to be deleted

Actual result

  1. Image not deleted
  2. Error displayed: The image cannot be removed as it has been assigned to the other image role
Catalog Format is valid bug report

Most helpful comment

We have also experienced this issue with the same configuration as above.
This only occurs when you have multiple stores and you have selected "All Store Views".
We got round it by selected each store and deleting.
https://gyazo.com/5d7070f6804d2d3c2fa3d357c0df1002

All 38 comments

We have also experienced this issue with the same configuration as above.
This only occurs when you have multiple stores and you have selected "All Store Views".
We got round it by selected each store and deleting.
https://gyazo.com/5d7070f6804d2d3c2fa3d357c0df1002

I have the same issue on the same version.

We have same issue as well on 2.1.3. Issue with our data seems to be related to migrated products with existing images. New products are working as expected as well as existing products without images but migrated products with existing images are not allowing us to add or remove images. Looking at the media tables all of the images for the product have the proper store scope (0) set and there are no references in the catalog_product_entity_varchar to other entries with store_id for the base, thumbnail, swatch, images etc.

Hi Dave,
I solved following the workaround of editing the products through the Store and/or through the Website INSTEAD OF through the Store_View.

---- On Mon, 23 Jan 2017 23:39:35 +0100 [email protected] wrote ----

We have same issue as well on 2.1.3. Issue with our data seems to be related to migrated products with existing images. New products are working as expected as well as existing products without images but migrated products with existing images are not allowing us to add or remove images. Looking at the media tables all of the images for the product have the proper store scope (0) set and there are no references in the catalog_product_entity_varchar to other entries with store_id for the base, thumbnail, swatch, images etc.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

Our issue was unrelated to image roles or specific store views.

~ line 179
vendor/magento/module-catalog/Controller/Adminhtml/Product/Save.php

 /**
 * Notify customer when image was not deleted in specific case.
 * TODO: temporary workaround must be eliminated in MAGETWO-45306
 *
 * @param array $postData
 * @param int $productId
 * @return void
 */

private function handleImageRemoveError($postData, $productId)

...

This method seems to be a very loose way to handle the image saving exceptions and is on the to do list.

vendor/magento/module-catalog/Model/Product/Gallery/CreateHandler

is where I tracked down that my "media_gallery" attribute code was returning empty values.

Looking in the database in catalog_product_entity_varchar (for which in my case is row_id 82 (media_gallery)) I found empty attribute records for ~1700 products. This is most likely from our data migration process.

this gave me the problem records:

 SELECT *  FROM catalog_product_entity_varchar WHERE attribute_id = 82;

Seems as though magento is using

catalog_product_entity_media_gallery 
catalog_product_entity_media_gallery_value
catalog_product_entity_media_gallery_value_to_entity

to store this data instead. The values in

catalog_product_entity_varchar

(in my case) were overriding the data objects

I have the same issue on the Magento ver. 2.0.8 EE. New products are working as expected.But Imported product with media images are not allowing us to add or remove images.

Not sure about 2.0.8 EE. We are on 2.1.3 EE. If you have a product attribute with a code of "media_gallery", check catalog_product_entity_varchar table in your database for whatever that attribute_id is (in my case it was 82) . Just to be safe you could delete only null values but my case all records for this attribute were null.

 SELECT * FROM  `catalog_product_entity_varchar` WHERE `attribute_id` = 82 AND `value` IS NULL;

If you have results with null values than delete those records from the table. Of coarse this comes with no garuntees (back your stuff up).

DELETE FROM  `catalog_product_entity_varchar` WHERE `attribute_id` = 82 AND `value` IS NULL;

Thanks Dave . I have checked 'media_gallery' attribute in catalog_product_entity_varchar table .There is no entry with that attribute. Do I need to provide default value for that attribute as Null ?

We have experienced this problem. If a product's image is edited/changed from the "All Store Views" mode, it doesn't actually update all store views, it only updates the store that is set as default.
The temporary work around is to upload the replacement picture in All Store Views and assign the required roles, save it. Then edit the product in the non-default store views, and change the roles to the new image in that view, and then delete the old image. This allows the picture to be replaced.

This is clearly a bug, if you edit a picture in the "All Store Views" layer, it should edit them in... well ALL STORE VIEWS!!!

Same issue on 2.1.4 and I completely agree with @spyrule

Exact same issue on 2.1.4

Same issue on 2.1.5

And on 2.1.7 :(

same on 2.1.7 .. wow.

@antboiko Any updates on this?

Any updates?

Same issue on 2.1.7

@vivek291186 Try to change the store view while saving the product. It will work then.

10548 should have fixed this issue

Thank you all who participate in this issue discussion and sorry for the delay with a response. Your feedback helps to review current media gallery implementation and define how it should be fixed and improved.

Some of the media gallery behavior aspects described in comments are bugs but some are not. Desired media gallery behavior, actual issues and required fixes described at magento/magento2#10863.


@craigcarnell, Magento by design does not allow remove an image if it has some special role (is a product image, small image or thumbnail) in store view that is not currently edited.

@spyrule, @hezide All Store Views scopes selector at product admin page correspond to default values for all store views. Saving a product with checked All Store Views change only value in store view with id=0. If another store view defines own value for the attribute it will not be overridden even if it is equal to a previous default value.

@itg-ddanielson, @shanthibalagurusamy I was not able to reproduce the issue with corrupting media gallery after Magento upgrade or products import. Please provide more details how your issues may be reproduced and fill free to create new issues regarding media gallery import and upgrade.

@vkublytskyi Normally I would agree with you. However this bug is easy to replicate. Attach an image at the default store view, save the product. Then attach a second image. Assign that image all 4 roles (base, small, thumbnail, swatch). Then delete the old image and now try to SAVE the product. It won't save because the old image roles are still applied to the original image in one of the other store views. This ISN'T how ALL STORE VIEWS is supposed to work (especially since, if you followed my instructions, you MADE NO SPECIFIC CHANGES to any other store view). So it IS a bug. (there is also a reason why it got thumbed up 5 times for a single comment...)

@spyrule I was not able to reproduce the issue with provided steps (checked on 2.1.8 and develop EE).

My environment:

  • Single website
  • two stores
  • each store has two enabled store views

Scenario 1

  • On Products > Catalog admin page click Add Product > Simple product
  • Specify only required fields and click Save button
  • After page reload, open Images and Videos tab, click on Browse to find or drag image here, select image, click on Save button to save product
  • After page reload, open Images and Videos tab, click on Browse to find or drag image here, select another image, click on added image preview, in opened image properties form check all available roles, close image properties form, click on trash icon to delete previous image, click Save product button.
  • Operation success, only single image left in gallery

Scenario 2

  • On Products > Catalog admin page click Add Product > Simple product
  • Specify only required fields and click Save button
  • After page reload, set Default Store View in scope switcher (previous value All Store Views)
  • After page reload, open Images and Videos tab, click on Browse to find or drag image here, select image, click on Save button to save product
  • After page reload, open Images and Videos tab, click on Browse to find or drag image here, select another image, click on added image preview, in opened image properties form check all available roles, close image properties form, click on trash icon to delete the previous image, click Save product button.
  • Operation success, only single image left in gallery

Please verify, if your STR is actual and if you are able to reproduce please provide more detailed steps.

@itg-ddanielson , Its great the people are able to fix this based on your suggestion. But, since I'm very new to Magento and my almost second task is to fix this issue, it's a bit a tough for me to fix this even though with your great help.

After migration from M1 to M2, we are not able to add or delete new images on a single product page. It is giving me the error (The image cannot be removed as it has been assigned to the other image role)

I have media_gallery attribute ID, product ID and DB access in my hand. Please guide me to fix this issue.

I tried by changing the store view to All Store Views, and single view (English version) but didn't get it fixed.

Any help is very much appreciated. Thanks in advance.

@itg-ddanielson,
Fixed it!
I changed in the catalog_product_entity_varchar table for each entity's store ID from 0 (admin) to 1 (default) and I can able to edit the images and add a new one.

But not sure if there is any loophole in it by doing this. Please help.

thanks @itg-ddanielson '@vkublytskyi
@MounikaK999 , @webspeaks @eduard-kistner @robbanl

Fixed it!!!
I experienced this issue after migrating from M1 to M2. When edit any product, add/delete image does not work. It shows when it is uploaded but after save it does not save. I have applied this query to solved the issue. here media_gallery attribute id is 703.

DELETE FROM catalog_product_entity_varchar WHERE attribute_id = 703 and value= 0

I want to add that this showed up for us and the issue ended up being that an older deactivated store still had settings saved for Base, Small, Thumbnail for the image we were attempting to delete. Once these were removed, we were able to delete the image just fine.

I experienced the inability to add or delete images after migrating M1 data to a 2.2.3 installation. My values in catalog_product_entity_varchar weren't empty or zero. After looking at some other Magento 2 installations (including 2.1.12 and 2.2.5), it appears that M2 doesn't store this attribute in the varchar table under normal circumstances. The backend type for the media_gallery attribute is "static", meaning it has special handling (i.e. the values are stored in various media_gallery tables instead).

In my migrated site, the backend type is "varchar" and the varchar attribute values were carried over. I ran the following query to delete media_gallery entries in catalog_product_entity_varchar completely, and I'm able to edit images again.

DELETE v FROM catalog_product_entity_varchar AS v INNER JOIN eav_attribute AS e ON v.attribute_id = e.attribute_id AND e.attribute_code = 'media_gallery' AND e.entity_type_id = 4

thanks @zaximus84, it works for me after running your query.

i solved this by first deselect all the options image,small_image,thumbnail in every store and save the product , after that i was able to delete it in a normal way without messing in the database.

You can change product image attributes (thumbnail,image, swatch_image, base_image) scope from store view to Global if you want same images on all store views. By doing this you will be able to change image from any store view and images will be same on all other store views.

I am facing same issue in magento 2.3 release.

Same issue with 2.3.2
But manually deleting the media_gallery attribute in catalog_product_entity_varchar that was NULL solved the problem.

Magento 2.3.4 => Problem still exist. What important. I had here 3x website. Product was add only to 1-st website. To use this workaround I had to active it in the rest of the websites, uncheck there roles and then remove the image.

Problem: Magento won't allow you to delete an image which is in use on another store, even if that store is not enabled for that specific product currently

Solution: Remove non-enabled store-specific product image selections

USE AT YOUR OWN RISK

--- Remove image selections from stores which a product isn't listed
DELETE e
FROM `catalog_product_entity_varchar` AS e
LEFT JOIN `catalog_product_website` AS w ON e.entity_id = w.product_id
    AND e.store_id = w.website_id
WHERE store_id > 0
  AND `attribute_id` >= '84'
  AND `attribute_id` <= '86'
  AND website_id IS NULL
  AND value != 'no_selection';

I am having this issue as well on 2.3.5-p1 and used the data-migration tool. The other thing you may want to check is if your sitemap is generating. I am receiving an error generating my sitemap, which I believe could be tied to this issue.

This solution worked to allow me to delete images, however there is still something amiss in the database values. Someone that is having this issue, please go to Marketing--Sitemap and generate a sitemap.xml. I get a blank screen that does not do anything and the sitemap is not generated. It is shown in apache error logs with this: AH01071: Got error 'PHP message: PHP Fatal error:
Uncaught TypeError: Argument 1 passed to Magento\Catalog\Model\Product\Image\UrlBuilder::getUrl() must be of the type string, null given.........

This is not related, please accept my apologies. There was actually a NULL value in catalog_product_entity_media_gallery that was migrated from M1, not due to the migration process.

I experienced the inability to add or delete images after migrating M1 data to a 2.2.3 installation. My values in catalog_product_entity_varchar weren't empty or zero. After looking at some other Magento 2 installations (including 2.1.12 and 2.2.5), it appears that M2 doesn't store this attribute in the varchar table under normal circumstances. The backend type for the media_gallery attribute is "static", meaning it has special handling (i.e. the values are stored in various media_gallery tables instead).

In my migrated site, the backend type is "varchar" and the varchar attribute values were carried over. I ran the following query to delete media_gallery entries in catalog_product_entity_varchar completely, and I'm able to edit images again.

DELETE v FROM catalog_product_entity_varchar AS v INNER JOIN eav_attribute AS e ON v.attribute_id = e.attribute_id AND e.attribute_code = 'media_gallery' AND e.entity_type_id = 4

working cool

2.3.5-p2 here we are again...

Was this page helpful?
0 / 5 - 0 ratings