Today I stumbled upon an issue in Magento which I couldn’t wrap my head around: In the backend I checked an image to be removed from the media gallery, but upon saving, nothing happened. Magento didn’t show a message and my logs were empty.
After a while of looking what was going wrong, I finally found out what was causing the problem. So I hope this article might save someone out there the same frustration I had.
Multiple store views
The problem was that I had multiple store views in my shop, and one of those views had a slightly different configuration than the default configuration:
Resetting the checkboxes to ‘Use Default Value’ fixed the problem. Now I was able to remove this image (from the default store view).
What’s happening under the hood?
The reason for this feature is the afterSave() -method in Mage_Catalog_Model_Product_Attribute_Backend_Media . This method generates an array called $picturesInOtherStores which is exactly what it says it is: an array of images that are used in other store views. Now, the code that checks for removal is the first part of the foreach() -loop in the method:
1 2 3 4 5 6 7 |
foreach ($value['images'] as &$image) { if(!empty($image['removed'])) { if(isset($image['value_id']) && !isset($picturesInOtherStores[$image['file']])) { $toDelete[] = $image['value_id']; } continue; } |
As you can see, it checks if an image is flagged for removal but it also checks if the image is not used in a different store view. If so, it just ignores the remove-flag, but it also doesn’t give you any message whatsoever about why the image isn’t removed! Silly Magento…
So there you have it! That’s why your image isn’t removed from the backend in Magento. I hope this article saved you the time I spent on tracking down this ‘feature’.
Visitors give this article an average rating of 4.6 out of 5.
How would you rate this article?
★ ★ ★ ★ ★
Hello, nice trick. I have the same problem when I try to delete all the product images from code. All images but one are removed. I haven’t yet found the way to do the same programmatically. Instead, following your topic, I was able finally to do directly from Magento backend. Regards,
I solved overriding the method in a custom rewrite block, removing the following code:
!isset($picturesInOtherStores[$image[‘file’]]), also if this change the default behavior from the backend (it was not my original purpose).
Regards,
I sell items that are one-off products. So when they sell on the other site, I don’t need them to even remain in the Magento database. I need the server space or really the file count needs to stay low for their autobackup to run.
So I’m deleting products and realize that it’s not helping me because deleting the product doesn’t delete the image. So for every product I remove, I’ll have to go delete the image via ftp. Oh joy.
Thank you for share this post. I have the same issue related with one of the storeviews.
Is there any sql sentence that can reset in all the storeviews the checkboxes to ?Use Default Value??
Thank you!
Perhaps deleting all rows from
catalog_product_entity_media_gallery_value
where the store_id != 0?Thank you Giel! I understand that I need to replace the sotre_id=0 with the number of the stores that have images assigend different than “0”. I’m right? Thanks again.
Yes,
store_id != 0
stands for all store id’s that don’t equal zero. But remember: cool kids always backup first! 😉oohh I understand! thank you so much Giel! Yes, I have very limited knowledge, for this reason I make all things first in staging server and I will make the mysql backup in live server too before changes 😀
Thanks for sharing this tips. I had the same problem, fixed by following your tip. Thank you again!
Hi
when i am trying to delete product image from magento backend i got error decode syntax error even i have trying with store views also same error occurred.Can you please help me on this
Error:Decoding failed: Syntax error
Thanks,
Chanikya
This saved me with a client. THANK YOU!
I was going to delete the product and import it again, thinking that was a problem with Magento and its DB, but fortunately I found your post.
Thank you!!!
so, how to edit that code to make Magento delete images in both database & FTP (when checked “Remove Image”)?
You better use a module for this: https://www.magentocommerce.com/magento-connect/image-clean.html
Thanks Giel, Your post was very help full for me
Thanks a lot Giel! This post was very helpful for me too.
Great information! Thank you, saved from my time. All the best!