cancel
Showing results for 
Search instead for 
Did you mean: 

Delete Custom option uploaded files

Delete Custom option uploaded files

Hi everybody and Happy new year !

 

I have a question about files the customer can upload in the the custom product options.

 

In the BO, we can find them only in the orders and bounds. So we can directly upload them to our computers by clicking on the link. But how can we delete them from the BO ?

 

I have found the location of the files in pub/media/custom_options/quote

 

Can I delete theme here without any risks ? Is it the only solution ?

 

The problem is that I am looking for an easy way for the user to upload them and delete them after. Do you know a way to upload the files in a cloud or something like that to manage all these files ?

 

Thanks for your help,

 

best regards

2 REPLIES 2

Re: Delete Custom option uploaded files

I have spent 2 weeks trying to find an answer to the file location and how to clean out customer uploaded files. I would be very interested in this answer.

Re: Delete Custom option uploaded files

Hello @nicksbpgrab670 

 

You can create a custom script to automate the deletion of these files. Below is an example script that you can run from the Magento root directory.

 

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$productRepository = $obj->get('Magento\Catalog\Api\ProductRepositoryInterface');
$productCollectionFactory = $obj->get('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$fileSystem = $obj->get('Magento\Framework\Filesystem');
$directory = $fileSystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$productCollection = $productCollectionFactory->create();
$productCollection->addAttributeToSelect('*');
foreach ($productCollection as $product) {
    $productId = $product->getId();
    $product = $productRepository->getById($productId);
    $options = $product->getOptions();
    foreach ($options as $option) {
        if ($option->getType() == 'file') {
            foreach ($option->getValues() as $value) {
                $fileName = $value->getTitle(); 
                $filePath = 'custom_options/' . $fileName;
                if ($directory->isFile($filePath)) {
                    $directory->delete($filePath);
                    echo "Deleted file: " . $filePath . "\n";
                }
            }
        }
    }
}
echo "Custom option file cleanup completed.\n";

Additional Considerations:

        Backup: Always backup your files and database before running any script that modifies or deletes data.

         Error Handling: Implement error handling and logging to ensure you can track and troubleshoot any issues that arise during the file deletion process.

 

Hope it helps !

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

 

 

 

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9