2

Magento 2.4.5-p1

I have some bad data in the database which I imported from Magento 1.9 via System -> Data Transfer -> Import

What happens is - when I go into:

- Catalog - Products

It gives the following Error:

X Something went wrong with processing the default view and we have restored the filter to its original state.

I've been through and around trying to identify which products are causing the problem but I can't get that done so I've decided to delete several pages of products and add them in manually.

When I set the filter to 20 per page - I am able to go through all pages one by one all the way up to page 115 out of 119 (that's where the Error starts).

I've tried truncating via:

TRUNCATE importexport_importdata;

but even this no longer works.

How would I delete all products from pages 115, 116, 117, 118 and 119 when I can't actually load those pages in Catalog - Products?

The last product on page 114 has an ID of 2282. I have access to phpMyAdmin.


UPDATED on October 6th, 2023:

Here are my logs:

Debug.log:

#0 /home/MYSITE/public_html/vendor/magento/module-catalog/Ui/Component/Listing/Columns/ProductActions.php(61): Magento\Framework\App\ErrorHandler->handler(2, 'Undefined array...', '/home/MYSITE/p...', 61)
#1 /home/MYSITE/public_html/vendor/magento/framework/View/Element/UiComponent/Context.php(396): Magento\Catalog\Ui\Component\Listing\Columns\ProductActions->prepareDataSource(Array)
#2 /home/MYSITE/public_html/vendor/magento/framework/View/Element/UiComponent/Context.php(393): Magento\Framework\View\Element\UiComponent\Context->prepareDataSource(Array, Object(Magento\Catalog\Ui\Component\Listing\Columns\ProductActions))
#3 /home/MYSITE/public_html/vendor/magento/framework/View/Element/UiComponent/Context.php(393): Magento\Framework\View\Element\UiComponent\Context->prepareDataSource(Array, Object(Magento\Catalog\Ui\Component\Listing\Columns\Interceptor))
#4 /home/MYSITE/public_html/vendor/magento/framework/View/Element/UiComponent/Context.php(251): Magento\Framework\View\Element\UiComponent\Context->prepareDataSource(Array, Object(Magento\Ui\Component\Listing))
#5 /home/MYSITE/public_html/vendor/magento/framework/View/Element/UiComponent/ContentType/Json.php(69): Magento\Framework\View\Element\UiComponent\Context->getDataSourceData(Object(Magento\Ui\Component\Listing))
#6 /home/MYSITE/public_html/vendor/magento/module-ui/Component/AbstractComponent.php(158): Magento\Framework\View\Element\UiComponent\ContentType\Json->render(Object(Magento\Ui\Component\Listing), 'templates/listi...')
#7 /home/MYSITE/public_html/vendor/magento/module-ui/Controller/Adminhtml/Index/Render.php(91): Magento\Ui\Component\AbstractComponent->render()
#8 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Ui\Controller\Adminhtml\Index\Render->execute()
#9 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->___callParent('execute', Array)
#10 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->Magento\Framework\Interception\{closure}()
#11 /home/MYSITE/public_html/generated/code/Magento/Ui/Controller/Adminhtml/Index/Render/Interceptor.php(23): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->___callPlugins('execute', Array, Array)
#12 /home/MYSITE/public_html/vendor/magento/framework/App/Action/Action.php(111): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->execute()
#13 /home/MYSITE/public_html/vendor/magento/module-backend/App/AbstractAction.php(151): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#14 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Backend\App\AbstractAction->dispatch(Object(Magento\Framework\App\Request\Http))
#15 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->___callParent('dispatch', Array)
#16 /home/MYSITE/public_html/vendor/magento/module-backend/App/Action/Plugin/Authentication.php(145): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#17 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch(Object(Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#18 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#19 /home/MYSITE/public_html/generated/code/Magento/Ui/Controller/Adminhtml/Index/Render/Interceptor.php(41): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->___callPlugins('dispatch', Array, Array)
#20 /home/MYSITE/public_html/vendor/magento/framework/App/FrontController.php(245): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#21 /home/MYSITE/public_html/vendor/magento/framework/App/FrontController.php(212): Magento\Framework\App\FrontController->getActionResponse(Object(Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor), Object(Magento\Framework\App\Request\Http))
#22 /home/MYSITE/public_html/vendor/magento/framework/App/FrontController.php(147): Magento\Framework\App\FrontController->processRequest(Object(Magento\Framework\App\Request\Http), Object(Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor))
#23 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#24 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#25 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#26 /home/MYSITE/public_html/generated/code/Magento/Framework/App/FrontController/Interceptor.php(23): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#27 /home/MYSITE/public_html/vendor/magento/framework/App/Http.php(116): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#28 /home/MYSITE/public_html/generated/code/Magento/Framework/App/Http/Interceptor.php(23): Magento\Framework\App\Http->launch()
#29 /home/MYSITE/public_html/vendor/magento/framework/App/Bootstrap.php(264): Magento\Framework\App\Http\Interceptor->launch()
#30 /home/MYSITE/public_html/pub/index.php(30): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#31 {main} [] []
[2023-10-06T09:09:26.971707+00:00] main.INFO: Broken reference: the 'catalog.compare.sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []
[2023-10-06T09:09:26.971926+00:00] main.INFO: Broken reference: the 'sale.reorder.sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []
[2023-10-06T09:09:26.972086+00:00] main.INFO: Broken reference: the 'wishlist_sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []
[2023-10-06T09:09:26.972190+00:00] main.INFO: Broken reference: the 'sw.sidenav' element cannot be added as child to 'sidebar.main', because the latter doesn't exist [] []
[2023-10-06T09:09:26.972257+00:00] main.INFO: Broken reference: the 'sidebar_second_block' element cannot be added as child to 'sidebar.second', because the latter doesn't exist [] []
[2023-10-06T09:09:26.972399+00:00] main.INFO: Broken reference: the 'currency' tries to reorder itself towards 'store_language', but their parents are different: 'header.panel' and 'porto_header' respectively. [] []
[2023-10-06T09:09:26.972493+00:00] main.INFO: Broken reference: the 'catalog.compare.link' tries to reorder itself towards 'top.links', but their parents are different: 'header.links' and 'header.panel' respectively. [] []
[2023-10-06T09:09:26.972611+00:00] main.INFO: Broken reference: the 'sw.sidenav' tries to reorder itself towards 'category_leftnav', but their parents are different: 'sidebar.main' and '' respectively. [] []
[2023-10-06T09:09:26.972675+00:00] main.INFO: Broken reference: the 'sidebar_second_block' tries to reorder itself towards '', but their parents are different: 'sidebar.second' and '' respectively. [] []

System.log:

Stack trace:
#0 /home/MYSITE/public_html/vendor/magento/module-catalog/Ui/Component/Listing/Columns/ProductActions.php(61): Magento\Framework\App\ErrorHandler->handler(2, 'Undefined array...', '/home/MYSITE/p...', 61)
#1 /home/MYSITE/public_html/vendor/magento/framework/View/Element/UiComponent/Context.php(396): Magento\Catalog\Ui\Component\Listing\Columns\ProductActions->prepareDataSource(Array)
#2 /home/MYSITE/public_html/vendor/magento/framework/View/Element/UiComponent/Context.php(393): Magento\Framework\View\Element\UiComponent\Context->prepareDataSource(Array, Object(Magento\Catalog\Ui\Component\Listing\Columns\ProductActions))
#3 /home/MYSITE/public_html/vendor/magento/framework/View/Element/UiComponent/Context.php(393): Magento\Framework\View\Element\UiComponent\Context->prepareDataSource(Array, Object(Magento\Catalog\Ui\Component\Listing\Columns\Interceptor))
#4 /home/MYSITE/public_html/vendor/magento/framework/View/Element/UiComponent/Context.php(251): Magento\Framework\View\Element\UiComponent\Context->prepareDataSource(Array, Object(Magento\Ui\Component\Listing))
#5 /home/MYSITE/public_html/vendor/magento/framework/View/Element/UiComponent/ContentType/Json.php(69): Magento\Framework\View\Element\UiComponent\Context->getDataSourceData(Object(Magento\Ui\Component\Listing))
#6 /home/MYSITE/public_html/vendor/magento/module-ui/Component/AbstractComponent.php(158): Magento\Framework\View\Element\UiComponent\ContentType\Json->render(Object(Magento\Ui\Component\Listing), 'templates/listi...')
#7 /home/MYSITE/public_html/vendor/magento/module-ui/Controller/Adminhtml/Index/Render.php(91): Magento\Ui\Component\AbstractComponent->render()
#8 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Ui\Controller\Adminhtml\Index\Render->execute()
#9 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->___callParent('execute', Array)
#10 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->Magento\Framework\Interception\{closure}()
#11 /home/MYSITE/public_html/generated/code/Magento/Ui/Controller/Adminhtml/Index/Render/Interceptor.php(23): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->___callPlugins('execute', Array, Array)
#12 /home/MYSITE/public_html/vendor/magento/framework/App/Action/Action.php(111): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->execute()
#13 /home/MYSITE/public_html/vendor/magento/module-backend/App/AbstractAction.php(151): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#14 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Backend\App\AbstractAction->dispatch(Object(Magento\Framework\App\Request\Http))
#15 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->___callParent('dispatch', Array)
#16 /home/MYSITE/public_html/vendor/magento/module-backend/App/Action/Plugin/Authentication.php(145): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#17 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch(Object(Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#18 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#19 /home/MYSITE/public_html/generated/code/Magento/Ui/Controller/Adminhtml/Index/Render/Interceptor.php(41): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->___callPlugins('dispatch', Array, Array)
#20 /home/MYSITE/public_html/vendor/magento/framework/App/FrontController.php(245): Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#21 /home/MYSITE/public_html/vendor/magento/framework/App/FrontController.php(212): Magento\Framework\App\FrontController->getActionResponse(Object(Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor), Object(Magento\Framework\App\Request\Http))
#22 /home/MYSITE/public_html/vendor/magento/framework/App/FrontController.php(147): Magento\Framework\App\FrontController->processRequest(Object(Magento\Framework\App\Request\Http), Object(Magento\Ui\Controller\Adminhtml\Index\Render\Interceptor))
#23 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#24 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#25 /home/MYSITE/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#26 /home/MYSITE/public_html/generated/code/Magento/Framework/App/FrontController/Interceptor.php(23): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#27 /home/MYSITE/public_html/vendor/magento/framework/App/Http.php(116): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#28 /home/MYSITE/public_html/generated/code/Magento/Framework/App/Http/Interceptor.php(23): Magento\Framework\App\Http->launch()
#29 /home/MYSITE/public_html/vendor/magento/framework/App/Bootstrap.php(264): Magento\Framework\App\Http\Interceptor->launch()
#30 /home/MYSITE/public_html/pub/index.php(30): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#31 {main} [] []
[2023-10-06T09:09:26.971707+00:00] main.INFO: Broken reference: the 'catalog.compare.sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []
[2023-10-06T09:09:26.971926+00:00] main.INFO: Broken reference: the 'sale.reorder.sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []
[2023-10-06T09:09:26.972086+00:00] main.INFO: Broken reference: the 'wishlist_sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []
[2023-10-06T09:09:26.972190+00:00] main.INFO: Broken reference: the 'sw.sidenav' element cannot be added as child to 'sidebar.main', because the latter doesn't exist [] []
[2023-10-06T09:09:26.972257+00:00] main.INFO: Broken reference: the 'sidebar_second_block' element cannot be added as child to 'sidebar.second', because the latter doesn't exist [] []
[2023-10-06T09:09:26.972399+00:00] main.INFO: Broken reference: the 'currency' tries to reorder itself towards 'store_language', but their parents are different: 'header.panel' and 'porto_header' respectively. [] []
[2023-10-06T09:09:26.972493+00:00] main.INFO: Broken reference: the 'catalog.compare.link' tries to reorder itself towards 'top.links', but their parents are different: 'header.links' and 'header.panel' respectively. [] []
[2023-10-06T09:09:26.972611+00:00] main.INFO: Broken reference: the 'sw.sidenav' tries to reorder itself towards 'category_leftnav', but their parents are different: 'sidebar.main' and '' respectively. [] []
[2023-10-06T09:09:26.972675+00:00] main.INFO: Broken reference: the 'sidebar_second_block' tries to reorder itself towards '', but their parents are different: 'sidebar.second' and '' respectively. [] []


UPDATED on October 7th, 2023:

I tried to run the script provided by Nikhil Ajani but I get the following Error. I even tried changing the catalog_product_entity to mage_catalog_product_entity because I believe that would be correct in my case:

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%)' at line 1 in /home/MYSITE/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:90 Stack trace: #0 /home/MYSITE/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(90): PDOStatement->execute() #1 /home/MYSITE/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(106): Magento\Framework\DB\Statement\Pdo\Mysql->Magento\Framework\DB\Statement\Pdo{closure}() #2 /home/MYSITE/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(91): Magento\Framework\DB\Statement\Pdo\Mysql->tryExecute(Object(Closure)) #3 /home/MYSITE/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement.php(313): Magento\Framework\DB\Statement\Pdo\Mysql->_execute(Array) #4 /home/MYSITE/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array) #5 /home/MYSITE/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT main_ta...', Array) #6 /home/MYSITE/public_html/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(564): Zend_Db_Adapter_Pdo_Abstract->query('SELECT main_ta...', Array) #7 /home/MYSITE/public_html/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(634): Magento\Framework\DB\Adapter\Pdo\Mysql->_query('SELECT main_ta...', Array) #8 /home/MYSITE/public_html/generated/code/Magento/Framework/DB/Adapter/Pdo/Mysql/Interceptor.php(95): Magento\Framework\DB\Adapter\Pdo\Mysql->query(Object(Magento\Framework\DB\Select), Array) #9 /home/MYSITE/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(811): Magento\Framework\DB\Adapter\Pdo\Mysql\Interceptor->query(Object(Magento\Framework\DB\Select), Array) #10 /home/MYSITE/public_html/generated/code/Magento/Framework/DB/Adapter/Pdo/Mysql/Interceptor.php(1121): Zend_Db_Adapter_Abstract->fetchPairs(Object(Magento\Framework\DB\Select), Array) #11 /home/MYSITE/public_html/pub/DeleteProduct.php(35): Magento\Framework\DB\Adapter\Pdo\Mysql\Interceptor->fetchPairs(Object(Magento\Framework\DB\Select)) #12 /home/MYSITE/public_html/pub/DeleteProduct.php(9): DeleteProduct->__construct() #13 {main} Next Zend_Db_Statement_Exception: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%)' at line 1, query was: SELECT main_table.entity_id, main_table.skuFROMmage_catalog_product_entityASmain_tableWHERE (entity_id LIKE 2282%) in /home/MYSITE/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:109 Stack trace: #0 /home/MYSITE/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(91): Magento\Framework\DB\Statement\Pdo\Mysql->tryExecute(Object(Closure)) #1 /home/MYSITE/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement.php(313): Magento\Framework\DB\Statement\Pdo\Mysql->_execute(Array) #2 /home/MYSITE/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array) #3 /home/MYSITE/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECTmain_ta...', Array) #4 /home/MYSITE/public_html/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(564): Zend_Db_Adapter_Pdo_Abstract->query('SELECT main_ta...', Array) #5 /home/MYSITE/public_html/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(634): Magento\Framework\DB\Adapter\Pdo\Mysql->_query('SELECT main_ta...', Array) #6 /home/MYSITE/public_html/generated/code/Magento/Framework/DB/Adapter/Pdo/Mysql/Interceptor.php(95): Magento\Framework\DB\Adapter\Pdo\Mysql->query(Object(Magento\Framework\DB\Select), Array) #7 /home/MYSITE/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(811): Magento\Framework\DB\Adapter\Pdo\Mysql\Interceptor->query(Object(Magento\Framework\DB\Select), Array) #8 /home/MYSITE/public_html/generated/code/Magento/Framework/DB/Adapter/Pdo/Mysql/Interceptor.php(1121): Zend_Db_Adapter_Abstract->fetchPairs(Object(Magento\Framework\DB\Select), Array) #9 /home/MYSITE/public_html/pub/DeleteProduct.php(35): Magento\Framework\DB\Adapter\Pdo\Mysql\Interceptor->fetchPairs(Object(Magento\Framework\DB\Select)) #10 /home/MYSITE/public_html/pub/DeleteProduct.php(9): DeleteProduct->__construct() #11 {main} thrown in /home/MYSITE/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php on line 109



Updated on October 10th, 2023:

The initial script provided by Nikhil Ajani actually works:

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
ini_set('display_errors', 1);
ini_set('max_execution_time', "-1");
ini_set('memory_limit', "-1");
// ini_set('auto_detect_line_endings',TRUE);

$exporter = new DeleteProduct();

class DeleteProduct
{
// Initialize the Mage application
    function __construct()
    {

        $bootstrap = Bootstrap::create(BP, $_SERVER);
        $objectManager = $bootstrap->getObjectManager();

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

        $registry = $objectManager->get('\Magento\Framework\Registry');
        $registry->register('isSecureArea', true);

        $resourceConnection = $objectManager->create(\Magento\Framework\App\ResourceConnection::class);
        $connection = $resourceConnection->getConnection();

        $select = $connection->select()
                ->from(['main_table' => "mage_catalog_product_entity"], ["entity_id", "sku"])
                ->where("entity_id LIKE '2283%'");

        $unUsedProducts = $connection->fetchPairs($select);

        if (!count($unUsedProducts)) {
            echo "No Unused Product found........";
            exit;
        }

        $collection = $objectManager->create(\Magento\Catalog\Model\ProductFactory::class)
            ->create()
            ->getCollection()
            ->addFieldToFilter("entity_id", ["in", array_keys($unUsedProducts)]);

        foreach ($collection as $i => $product) {
            $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class)->delete($product);
        }
    }
}
2
  • Could you please review your Magento logs to obtain the error information for this? Oct 6, 2023 at 8:01
  • @GrazittiDev Hi Grazitti - I have uploaded my debug.log and system.log. These are the only 2 log files I have in my /public_html/var/log directory. Thank you.
    – Allysin
    Oct 6, 2023 at 9:16

2 Answers 2

2

For delete product that ID start with 2282. For that you can create root script to delete those products.

Create Below file in you Magento Pub folder

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
ini_set('display_errors', 1);
ini_set('max_execution_time', "-1");
ini_set('memory_limit', "-1");
// ini_set('auto_detect_line_endings',TRUE);

$exporter = new DeleteProduct();

class DeleteProduct
{
// Initialize the Mage application
    function __construct()
    {

        $bootstrap = Bootstrap::create(BP, $_SERVER);
        $objectManager = $bootstrap->getObjectManager();

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

        $registry = $objectManager->get('\Magento\Framework\Registry');
        $registry->register('isSecureArea', true);

        $resourceConnection = $objectManager->create(\Magento\Framework\App\ResourceConnection::class);
        $connection = $resourceConnection->getConnection();

        $select = $connection->select()
                ->from(['main_table' => "catalog_product_entity"], ["entity_id", "sku"])
                ->where("entity_id LIKE '2282%'");

        $unUsedProducts = $connection->fetchPairs($select);

        if (!count($unUsedProducts)) {
            echo "No Unused Product found........";
            exit;
        }

        $collection = $objectManager->create(\Magento\Catalog\Model\ProductFactory::class)
            ->create()
            ->getCollection()
            ->addFieldToFilter("entity_id", ["in", array_keys($unUsedProducts)]);

        foreach ($collection as $i => $product) {
            $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class)->delete($product);
        }
    }
}

You can run above code with URL http://127.0.0.1/magento246/pub/DeleteProduct.php Let me know if you need any help.

Thanks

10
  • Thank you for that. When you say to create root script - would I just make a file, put your code into it and then call that up from somewhere? Thank you.
    – Allysin
    Oct 6, 2023 at 9:53
  • I just saw the URL you added. So just to make sure I do this correctly = make a file called DeleteProduct.php and put it on the root level of my Magento with your code in it, then just go to that URL such as this: www.MYSITE.com/DeleteProduct.php - is that correct?
    – Allysin
    Oct 6, 2023 at 9:55
  • @Allysin, Yes you have to create new file with name DeleteProduct.php in your magento PUB directory and then you can access like above link you say. Oct 6, 2023 at 9:57
  • I tried running your script but I get an Error. Would you please take a look? I've posted the Error into my original post. Thank you so much.
    – Allysin
    Oct 7, 2023 at 12:02
  • @Allysin I have updated my script code. Could you please try once? I hope it works this time. Oct 9, 2023 at 4:41
0

you can try with this script, its a simple script to delete product you can modify accordingly

<?php
use Magento\Framework\AppInterface;

try {
    require_once __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}

try {
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('\Magento\Framework\App\State');
    $appState->setAreaCode('frontend');

    $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
    $registry = $objectManager->get('\Magento\Framework\Registry');

    $registry->register('isSecureArea', true);

    // Using product id to remove product
    $product_id = 1; // Replace with the product ID you want to delete
    $product = $productRepository->getById($product_id);
    $productRepository->delete($product);

    echo "Product with ID $product_id has been deleted successfully.";
} catch (\Exception $e) {
    print_r($e->getMessage());
}
3
  • Hi Pramod - thank you for your code. I'm not sure if I have done this correctly? I've put your code into a file which I called 'Delete2.php' and I uploaded this into the pub folder located in /public_html/pub/ Your code also refers to \Magento\Framework\App\.. Do I need to find where that is located on my install and change the path accordingly? My Magento is insalled in root/public_html/. I also have prefix of 'mage_' in my database. I've uploaded the file and when I go to the URL = MYSITE.com/pub/Delete2.php I get: HTTP This page isn’t working ERROR 500 Thank you for helping.
    – Allysin
    Oct 10, 2023 at 20:41
  • update this line require DIR . '/../app/bootstrap.php';
    – Pramod
    Oct 11, 2023 at 4:07
  • also if you are not able to run it through standalone script create a sample cron for test purpose and put try catch block in it. and then execute that cron here is a link for reference for cron mageplaza.com/devdocs/magento-2-create-cron-job
    – Pramod
    Oct 11, 2023 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.