Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting ALL products on Magento

Tags:

php

mysql

magento

There are a lot of posts regarding this issue on Magento. Most of them work but the problem with Categories not resetting their values to ZERO after executing the delete codes/SQL statements still persists and not being addressed up until now.

Does anyone here has the correct code or procedure to properly delete all products from the inventory and have the Category listing also reset to zero after executing it?

Thanks in advance.

like image 857
Jhourlad Estrella Avatar asked Jan 17 '12 17:01

Jhourlad Estrella


People also ask

How do I remove all images from Magento 2?

Find & Remove Unused Product Images in Magento 2 To clean unused product images in Magento 2, go to Image Clean > Unused Product Images. The backend grid – 'Unused Product Images' lists all the product images in the Magento 2 store that are not being used by any of the products.


2 Answers

In an stand alone script:

<?php
require 'app/Mage.php';
Mage::app('admin')->setUseSessionInUrl(false);


$products = Mage::getModel('catalog/product')->getCollection();    
foreach ($products as $product) {
    try {
        $product->delete();
    } catch(Exception $e) {
        echo "Product #".$product->getId()." could not be remvoved: ".$e->getMessage();
    }
}

Did that for more that 1400 products, worked fine. You have to do a reindexing after that.

Beware the above script will delete ALL your products

like image 32
Kervin Ramen Avatar answered Sep 23 '22 20:09

Kervin Ramen


Deleting all products from Magento can be easily achieved, just run:

DELETE FROM `catalog_product_entity`

Because of the foreign key constraints set up in Magento's database, all other tables that have product information in them are cleaned up nicely. It will of course take some time to delete a lot of products, but it at least gets cleaned up nicely.

If the query can't run because of maximum execution time, you can always run something like:

DELETE FROM `catalog_product_entity` LIMIT 10000

Update: this logic also gets used in Magento's core, so it's safe to use! https://github.com/OpenMage/magento-mirror/blob/magento-1.8/app/code/core/Mage/ImportExport/Model/Import/Entity/Product.php#L462

like image 183
Paul Hachmang Avatar answered Sep 22 '22 20:09

Paul Hachmang