17

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.

2
  • You mean the ID numbers? you'd have to reset the tables' auto_increment primary keys.
    – Marc B
    Commented Jan 17, 2012 at 18:02
  • No. I'm pertaining to the count for the products under each category. Commented Jan 17, 2012 at 18:21

9 Answers 9

59

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

6
  • this doesn't delete the categories
    – Claire
    Commented Jun 27, 2014 at 19:37
  • 5
    Indeed, it doesn't, this is meant to delete all products. Commented Jun 30, 2014 at 8:58
  • 3
    To delete categories, use: DELETE FROM catalog_category_entity; Commented Sep 5, 2014 at 9:58
  • 3
    This needs to be the top answer.
    – pspahn
    Commented Oct 29, 2014 at 0:01
  • 8
    I would NOT run DELETE FROM catalog_category_entity; because it deletes your root category, causing your some grief. maybe use DELETE FROM catalog_category_entity WHERE level != 0 Commented Dec 18, 2015 at 0:18
14

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

3
  • 1
    +1 for using Magento scripting which should preserve data integrity instead of attacking the database, finding things don't work and turning the integrity checks off to find later that things really don't work. Commented Mar 31, 2014 at 15:49
  • integrity issues on may products Commented Apr 6, 2014 at 15:39
  • For a larger number of products (~ 100000) the script can be improved by adding: ini_set("memory_limit", "2024M"); ini_set("max_execution_time", "86400"); and ` Mage::log('to delete: ' . $product-> getId());` for some feedback. And it might be senseful to start it in a screen. Commented Mar 21, 2016 at 15:29
6

Regardless of the method used to delete the products, rebuilding the "Category Products" index should fix the counts. Go to System > Index Management. Check the box next to "Category Products", change the action (top right) to "Reindex" and click submit.

1
  • I changed my mind. For the benefit of those who are having similar problem, I'm choosing this as the most suitable answer. Commented Feb 21, 2013 at 1:19
6

To delete all products you can use this query:

TRUNCATE TABLE `catalog_product_bundle_option`;
TRUNCATE TABLE `catalog_product_bundle_option_value`;
TRUNCATE TABLE `catalog_product_bundle_selection`;
TRUNCATE TABLE `catalog_product_entity_datetime`;
TRUNCATE TABLE `catalog_product_entity_decimal`;
TRUNCATE TABLE `catalog_product_entity_gallery`;
TRUNCATE TABLE `catalog_product_entity_int`;
TRUNCATE TABLE `catalog_product_entity_media_gallery`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`;
TRUNCATE TABLE `catalog_product_entity_text`;
TRUNCATE TABLE `catalog_product_entity_tier_price`;
TRUNCATE TABLE `catalog_product_entity_varchar`;
TRUNCATE TABLE `catalog_product_link`;
TRUNCATE TABLE `catalog_product_link_attribute`;
TRUNCATE TABLE `catalog_product_link_attribute_decimal`;
TRUNCATE TABLE `catalog_product_link_attribute_int`;
TRUNCATE TABLE `catalog_product_link_attribute_varchar`;
TRUNCATE TABLE `catalog_product_link_type`;
TRUNCATE TABLE `catalog_product_option`;
TRUNCATE TABLE `catalog_product_option_price`;
TRUNCATE TABLE `catalog_product_option_title`;
TRUNCATE TABLE `catalog_product_option_type_price`;
TRUNCATE TABLE `catalog_product_option_type_title`;
TRUNCATE TABLE `catalog_product_option_type_value`;
TRUNCATE TABLE `catalog_product_super_attribute`;
TRUNCATE TABLE `catalog_product_super_attribute_label`;
TRUNCATE TABLE `catalog_product_super_attribute_pricing`;
TRUNCATE TABLE `catalog_product_super_link`;
TRUNCATE TABLE `catalog_product_enabled_index`;
TRUNCATE TABLE `catalog_product_website`;
TRUNCATE TABLE `catalog_product_entity`;

TRUNCATE TABLE `cataloginventory_stock`;
TRUNCATE TABLE `cataloginventory_stock_item`;
TRUNCATE TABLE `cataloginventory_stock_status`;

INSERT  INTO `catalog_product_link_type`(`link_type_id`,`code`) VALUES (1,'relation'),(2,'bundle'),(3,'super'),(4,'up_sell'),(5,'cross_sell');
INSERT  INTO `catalog_product_link_attribute`(`product_link_attribute_id`,`link_type_id`,`product_link_attribute_code`,`data_type`) VALUES (1,2,'qty','decimal'),(2,1,'position','int'),(3,4,'position','int'),(4,5,'position','int'),(6,1,'qty','decimal'),(7,3,'position','int'),(8,3,'qty','decimal');
INSERT  INTO `cataloginventory_stock`(`stock_id`,`stock_name`) VALUES (1,'Default');

Regards :)

3
  • This query doesn't work due to data integrity issue. It shows me the error #1701 - Cannot truncate a table referenced in a foreign key constraint (dbname.catalog_product_bundle_option_value, CONSTRAINT FK_MKM_CAT_PRD_BNDL_OPT_VAL_OPT_ID_MKM_CAT_PRD_BNDL_OPT_OPT_ID` FOREIGN KEY (option_id) REFERENCES dbname.catalog_produ). Any idea to solve it?
    – Abaij
    Commented Jul 31, 2013 at 17:28
  • 3
    add SET FOREIGN_KEY_CHECKS=0; at top of the above code and add SET FOREIGN_KEY_CHECKS=1; bottom of code.
    – Zahirabbas
    Commented Nov 26, 2013 at 5:17
  • Remove checks for data integrity, cross your fingers and hope data corruption doesn't ensue = SET FOREIGN_KEY_CHECKS=0; Commented Mar 31, 2014 at 15:21
3

In order to delete products, you can simply use backend:
Catalog > Manage Products > Select all checboxes and choose Action = Delete and hit submit.
This may work very good for some thousands of products.

1
  • 1
    Well for that much products it will be really slow and memory/server timeout issues.
    – MagePsycho
    Commented Jan 18, 2012 at 16:53
3
Mage::getModel('catalog/product')->getCollection()->delete();
2

Worked like a charm...

Php myadmin - select databse (magento) SWL at top and then pasted the following:

DELETE FROM `catalog_product_entity`
1

Tested on Magento 1.7.0.2

    SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE `catalog_product_bundle_option`;
TRUNCATE TABLE `catalog_product_bundle_option_value`;
TRUNCATE TABLE `catalog_product_bundle_selection`;
TRUNCATE TABLE `catalog_product_entity_datetime`;
TRUNCATE TABLE `catalog_product_entity_decimal`;
TRUNCATE TABLE `catalog_product_entity_gallery`;
TRUNCATE TABLE `catalog_product_entity_int`;
TRUNCATE TABLE `catalog_product_entity_media_gallery`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`;
TRUNCATE TABLE `catalog_product_entity_text`;
TRUNCATE TABLE `catalog_product_entity_tier_price`;
TRUNCATE TABLE `catalog_product_entity_varchar`;
TRUNCATE TABLE `catalog_product_flat_1`;
TRUNCATE TABLE `catalog_product_link`;
TRUNCATE TABLE `catalog_product_link_attribute`;
TRUNCATE TABLE `catalog_product_link_attribute_decimal`;
TRUNCATE TABLE `catalog_product_link_attribute_int`;
TRUNCATE TABLE `catalog_product_link_attribute_varchar`;
TRUNCATE TABLE `catalog_product_link_type`;
TRUNCATE TABLE `catalog_product_option`;
TRUNCATE TABLE `catalog_product_option_price`;
TRUNCATE TABLE `catalog_product_option_title`;
TRUNCATE TABLE `catalog_product_option_type_price`;
TRUNCATE TABLE `catalog_product_option_type_title`;
TRUNCATE TABLE `catalog_product_option_type_value`;
TRUNCATE TABLE `catalog_product_super_attribute_label`;
TRUNCATE TABLE `catalog_product_super_attribute_pricing`;
TRUNCATE TABLE `catalog_product_super_attribute`;
TRUNCATE TABLE `catalog_product_super_link`;
TRUNCATE TABLE `catalog_product_enabled_index`;
TRUNCATE TABLE `catalog_product_website`;
TRUNCATE TABLE `catalog_product_relation`;
TRUNCATE TABLE `catalog_category_product_index`;
TRUNCATE TABLE `catalog_category_product`;
TRUNCATE TABLE `cataloginventory_stock_item`;
TRUNCATE TABLE `cataloginventory_stock_status`;
TRUNCATE TABLE `cataloginventory_stock_status_idx`;
TRUNCATE TABLE `cataloginventory_stock`;
TRUNCATE TABLE `core_url_rewrite`;
INSERT  INTO `catalog_product_link_type`(`link_type_id`,`code`) VALUES (1,'relation'),(2,'bundle'),(3,'super'),(4,'up_sell'),(5,'cross_sell');
INSERT  INTO `catalog_product_link_attribute`(`product_link_attribute_id`,`link_type_id`,`product_link_attribute_code`,`data_type`) VALUES (1,2,'qty','decimal'),(2,1,'position','int'),(3,4,'position','int'),(4,5,'position','int'),(6,1,'qty','decimal'),(7,3,'position','int'),(8,3,'qty','decimal');
INSERT  INTO `cataloginventory_stock`(`stock_id`,`stock_name`) VALUES (1,'Default');
TRUNCATE TABLE `catalog_product_entity`;
SET FOREIGN_KEY_CHECKS = 1;
2
  • 2
    Please never use queries that set the foreign key checks to 0. The queries run faster, but you are risking database corruption. Commented Oct 16, 2014 at 19:02
  • You can't truncate with FK on, because imho mysql deletes the table and creates a new one. Commented Aug 25, 2016 at 13:01
0

The cleanest solution in my opinion is this:

  • Go to System -> Index Management and set all indexes to "Manual Update" index mode
  • Go to Catalog -> Manage Products, select all products and use the Delete action to delete all products.

This is not as fast as manually deleting the products from the database but it's probably the fastest proper way to delete all products where you don't have to worry about orphaned database entries or foreign key conflicts.

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.