Magento2: Admin: While product which has images deleted. It's not deleting from Folder.

Created on 11 Oct 2016  路  19Comments  路  Source: magento/magento2

Preconditions

  1. I'm using Magento 2 CE Version 2.1.1

Steps to reproduce

  1. Go to Admin -> Products -> Catalog
  2. When Product has image & if you delete from listing.
  3. It will not delete product images from Folder.

Expected result

  1. When product is deleted, It should delete all product images from Folder as well.

Actual result

  1. Currently it's not deleting product Images.
  2. Due to Image Not Deleted, there will be lots of space on server will be used.
  3. This is the only case for Products, May be we have same issue of other Admin module as well where image is uploaded.
  4. Need to check whole Admin where image is related while Delete.
Catalog Clear Description Confirmed Format is valid Ready for Work Reproduced on 2.1.x Reproduced on 2.2.x bug report

Most helpful comment

I read somewhere that magento by design doesn't delete images from the folder when you remove from the admin.

The reasoning is that some images might be used elsewhere in the system and deleting would cause a chain reaction of missing images.

All 19 comments

I am also having the same issue.

Any luck?

Hi @kumarancontus Nope still issue? They didn't solve yet. Waiting for their Reply

I read somewhere that magento by design doesn't delete images from the folder when you remove from the admin.

The reasoning is that some images might be used elsewhere in the system and deleting would cause a chain reaction of missing images.

Thanks for your response.

I had done it like below and it worked fine. Please try in your end

Code :

public function removeImageForProduct($productId, $imagesIds) {
if (count ( $imagesIds ) >= 1) {
array_unique ( $imagesIds );
$objectManager = MagentoFrameworkAppObjectManager::getInstance ();
$product = $objectManager->get('MagentoCatalogModelProduct')->load($productId);
$images = $product->getMediaGalleryImages();

        $objectGallery = $objectManager->get ( 'Magento\Catalog\Model\ResourceModel\Product\Gallery' );
        $objectGallery->deleteGallery ( $imagesIds );
        $mediaDirectory = $objectManager->get('Magento\Framework\Filesystem')->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
        $mediaRootDir = $mediaDirectory->getAbsolutePath();
        foreach($imagesIds as $image){
            foreach($images as $productImage){

                if($productImage['id'] == $image){

                  $imageFilePath = $productImage['file'];

                  $mediaRootDirectory = $mediaRootDir.'catalog/product';
        if ($this->_file->isExists($mediaRootDirectory . $imageFilePath))  {

            $this->_file->deleteFile($mediaRootDirectory . $imageFilePath);
        } }
            }
        }
    }
}

Hi @kumarancontus Thanks will try. Just do formatting of your code. Thanks a lot for help

@kumarancontus can you explain how I can implement this code? I understand it's PHP, so I'll need to execute in the browser or shell in command line, but I'm new to this and don't know where to place the code itself. Sorry.

What file should it be place in?
Where should I place this in my /store directory?

@jmtakahashi please note that I have used the above mentioned code inside our custom module for deleting the images from server, so you need to use this code inside your module image delete operation function.

@kumarancontus ok, this seems to be beyond my scope of magento knowledge. I need to create a custom image delete module in my magento/app/code/[my_name]/[my_module_name] folder?

Then I place the code into a php file?

@jmtakahashi please let me know in which scenario you are facing the issue in deleting images in your end.

@kumarancontus I would like to delete all the images that are no longer being used in the catalog. ALL the sample products I would like to remove as well as the mistakes (i.e. multiple upload due to failed attempts). I looked for a solution, and I found code. but I don't know how to implement the code. I don't know where I should place the code. Should I create my own module then try to place this code in the module? do I look for existing file in Magento and add code?

I'm sorry.

Internal ticket MAGETWO-69947 was created;

@shahankitb997, thank you for your report.
We've created internal ticket(s) MAGETWO-69947 to track progress on the issue.

@kumarancontus Where to place this code, I want to achieve the same purpose as you.

@Robert-Damon

Kindly let me know whether you want to use this functionality for core Magento or inside your custom functionality.

Strange behavior for Magento2, already the same in Magento1...

From what I understood, Magento doesn't remove associated images when deleting a product in case of further needs but I still really don't understood possible issues.

My media/catalog/product folder size is almost 5gb for actually only 11.000 products in the db and almost a year in production (clothes, 4 collections/year).

With a custom Command Line for test, I found more than 3gb of old unrelated images (including the /cache folder)


What should be the best way to manage such disk usage without deleting this files ??

@magento-engcom-team Can we get a status update on this please?
@cap340 Would you be interested in submitting a PR with your fix? That's probably the best and fastest way for this to get fixed in core.

Hi @gulshanchitransh. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if your want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.

Hi @amitvishvakarma. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.


Unfortunately, we are archiving this ticket now as it did not get much attention from both Magento Community and Core developers for an extended period. This is done in an effort to create a quality, community-driven backlog which will allow us to allocate the required attention more easily.

Please feel free to comment or reopen according to the Issue reporting guidelines
the ticket if you are still facing this issue on the latest 2.x-develop branch. Thank you for collaboration.

Was this page helpful?
0 / 5 - 0 ratings