72

I am currently doing a lot of tests in Magento so I can get familiar with it. Unfortunately the database is extremely cluttered with my tests. I wish to clear up the database so I can start over again. This means doing the following:

  • Clearing all products
  • Clearing all attributes
  • Clearing all attribute sets
  • Clearing all categories
  • Clearing all orders, shipments, transactions etc.

I have found a post on the Magento forums for clearing the products, which is the following SQL:

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;

I would need the same for the others mentioned. Unfortunately I don't know which tables are holding all the different data, otherwise I would've done it by myself. I also don't want to clear the wrong tables by accident. I hope you can help me!

  • Which data from your testing database do you still need? Did you consider exporting that data (for example your configuration core_config_data) and just start over again? – Alex May 14 '13 at 8:36
  • 1
    Also have a look at github.com/netz98/n98-magerun#stripped-database-dump - this allows you to export the database without orders etc. and then you can re-import. – Alex May 14 '13 at 8:37
  • @Alex I'm not sure what to keep because I got this from a third party that made the base of the shop. I need to continue developing on it myself, but I'm not entirely sure what I can just throw away and what not. Also, I'll look in to the link you gave. – Deep Frozen May 14 '13 at 8:38
  • I accidently just cleared all of the attributes. Including the Magento System attributes. I can't create products now. Is there a way just to restore the default Magento core system attributes? – Jelly_Doughnut Nov 19 '13 at 20:59
  • @Jelly_Doughnut I would suggest asking a separate question – Kristof at Fooman Nov 19 '13 at 23:19

13 Answers 13

96
    ***********for categories********************
TRUNCATE TABLE `catalog_category_entity`;
TRUNCATE TABLE `catalog_category_entity_datetime`;
TRUNCATE TABLE `catalog_category_entity_decimal`;
TRUNCATE TABLE `catalog_category_entity_int`;
TRUNCATE TABLE `catalog_category_entity_text`;
TRUNCATE TABLE `catalog_category_entity_varchar`;
TRUNCATE TABLE `catalog_category_product`;
TRUNCATE TABLE `catalog_category_product_index`;

INSERT  INTO `catalog_category_entity`(`entity_id`,`entity_type_id`,`attribute_set_id`,`parent_id`,`created_at`,`updated_at`,`path`,`POSITION`,`level`,`children_count`) VALUES (1,3,0,0,'0000-00-00 00:00:00','2009-02-20 00:25:34','1',1,0,1),(2,3,3,0,'2009-02-20 00:25:34','2009-02-20 00:25:34','1/2',1,1,0);
INSERT  INTO `catalog_category_entity_int`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) VALUES (1,3,32,0,2,1),(2,3,32,1,2,1);
INSERT  INTO `catalog_category_entity_varchar`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) VALUES (1,3,31,0,1,'Root Catalog'),(2,3,33,0,1,'root-catalog'),(3,3,31,0,2,'Default Category'),(4,3,39,0,2,'PRODUCTS'),(5,3,33,0,2,'default-category');

    *****************for customers*****************


    SET FOREIGN_KEY_CHECKS=0;
-- reset customers
TRUNCATE customer_address_entity;
TRUNCATE customer_address_entity_datetime;
TRUNCATE customer_address_entity_decimal;
TRUNCATE customer_address_entity_int;
TRUNCATE customer_address_entity_text;
TRUNCATE customer_address_entity_varchar;
TRUNCATE customer_entity;
TRUNCATE customer_entity_datetime;
TRUNCATE customer_entity_decimal;
TRUNCATE customer_entity_int;
TRUNCATE customer_entity_text;
TRUNCATE customer_entity_varchar;
TRUNCATE log_customer;
TRUNCATE log_visitor;
TRUNCATE log_visitor_info;

ALTER TABLE customer_address_entity AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_datetime AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_decimal AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_int AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_text AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_varchar AUTO_INCREMENT=1;
ALTER TABLE customer_entity AUTO_INCREMENT=1;
ALTER TABLE customer_entity_datetime AUTO_INCREMENT=1;
ALTER TABLE customer_entity_decimal AUTO_INCREMENT=1;
ALTER TABLE customer_entity_int AUTO_INCREMENT=1;
ALTER TABLE customer_entity_text AUTO_INCREMENT=1;
ALTER TABLE customer_entity_varchar AUTO_INCREMENT=1;
ALTER TABLE log_customer AUTO_INCREMENT=1;
ALTER TABLE log_visitor AUTO_INCREMENT=1;
ALTER TABLE log_visitor_info AUTO_INCREMENT=1;
SET FOREIGN_KEY_CHECKS=1;

For Orders

    SET FOREIGN_KEY_CHECKS=0; 
TRUNCATE `sales_flat_creditmemo`; 
TRUNCATE `sales_flat_creditmemo_comment`; 
TRUNCATE `sales_flat_creditmemo_grid`; 
TRUNCATE `sales_flat_creditmemo_item`; 
TRUNCATE `sales_flat_invoice`; 
TRUNCATE `sales_flat_invoice_comment`; 
TRUNCATE `sales_flat_invoice_grid`; 
TRUNCATE `sales_flat_invoice_item`; 
TRUNCATE `sales_flat_order`; 
TRUNCATE `sales_flat_order_address`; 
TRUNCATE `sales_flat_order_grid`; 
TRUNCATE `sales_flat_order_item`; 
TRUNCATE `sales_flat_order_payment`; 
TRUNCATE `sales_flat_order_status_history`; 
TRUNCATE `sales_flat_quote`; 
TRUNCATE `sales_flat_quote_address`; 
TRUNCATE `sales_flat_quote_address_item`; 
TRUNCATE `sales_flat_quote_item`; 
TRUNCATE `sales_flat_quote_item_option`; 
TRUNCATE `sales_flat_quote_payment`; 
TRUNCATE `sales_flat_quote_shipping_rate`; 
TRUNCATE `sales_flat_shipment`; 
TRUNCATE `sales_flat_shipment_comment`; 
TRUNCATE `sales_flat_shipment_grid`; 
TRUNCATE `sales_flat_shipment_item`; 
TRUNCATE `sales_flat_shipment_track`; 
TRUNCATE `sales_invoiced_aggregated`; 
TRUNCATE `sales_invoiced_aggregated_order`; 
TRUNCATE `sales_payment_transaction`;
TRUNCATE `sales_order_aggregated_created`; 
TRUNCATE `sales_order_tax`;
TRUNCATE `sales_order_tax_item`;
TRUNCATE `sendfriend_log`; 
TRUNCATE `tag`; 
TRUNCATE `tag_relation`; 
TRUNCATE `tag_summary`; 
TRUNCATE `wishlist`; 
TRUNCATE `log_quote`; 
TRUNCATE `report_event`; 
ALTER TABLE `sales_flat_creditmemo` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_creditmemo_comment` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_creditmemo_grid` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_creditmemo_item` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_invoice_comment` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_quote_shipping_rate` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_shipment_comment` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1; 
ALTER TABLE `sales_flat_shipment_track` AUTO_INCREMENT=1; 
ALTER TABLE `sales_invoiced_aggregated` AUTO_INCREMENT=1; 
ALTER TABLE `sales_invoiced_aggregated_order` AUTO_INCREMENT=1; 
ALTER TABLE `sales_payment_transaction` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order_aggregated_created` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order_tax` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order_tax_item` AUTO_INCREMENT=1; 
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1; 
ALTER TABLE `tag` AUTO_INCREMENT=1; 
ALTER TABLE `tag_relation` AUTO_INCREMENT=1; 
ALTER TABLE `tag_summary` AUTO_INCREMENT=1; 
ALTER TABLE `wishlist` AUTO_INCREMENT=1; 
ALTER TABLE `log_quote` AUTO_INCREMENT=1; 
ALTER TABLE `report_event` AUTO_INCREMENT=1; 
SET FOREIGN_KEY_CHECKS=1;

You can use this function to delete attribute sets.

 $resource = Mage::getSingleton('core/resource');
$db_read = $resource->getConnection('core_read');

$attribute_sets = $db_read->fetchCol("SELECT attribute_set_id FROM " . $resource->getTableName("eav_attribute_set") . " WHERE attribute_set_id<> 4 AND entity_type_id=4");
foreach ($attribute_sets as $attribute_set_id) {
    try {
        Mage::getModel("eav/entity_attribute_set")->load($attribute_set_id)->delete();
    } catch (Exception $e) {
        echo $e->getMessage() . "\n";
    }
}

And look for this table to remove attribute

catalog_eav_attribute
eav_attribute
eav_attribute_set

Check eav_attribute column is_user_defined so you can get some idea.

  • This is exactly what I am looking for! Do you also have it for the attributes and attribute sets? – Deep Frozen May 14 '13 at 8:51
  • No i have no sql for attribute and attribute sets, you can manually delete your attribute and attribute sets if there are no much entry – Mufaddal May 14 '13 at 8:53
  • Unfortunately there are a lot of them. Anyway, thank you for these, it will certainly help! I'll just have to find them for the attributes by myself i guess. – Deep Frozen May 14 '13 at 8:55
  • @Rune Ok check my updated answer – Mufaddal May 14 '13 at 9:08
  • 2
    Note on Enterprise you also need to truncate: enterprise_customer_sales_flat_order enterprise_customer_sales_flat_order_address enterprise_sales_order_grid_archive – Tegan Snyder Jan 15 '15 at 18:59
18

You can just empty the whole database and Magento will run all its install/upgrade/data scripts again on the next pageload. This will "factory reset" Magento and will destroy all your changes.

A good habit is to create a module that contains all your basic store settings inside install/upgrade/data scripts. This has the benefit that these settings are stored in version control and all the different websites you have for the store (production, staging, user acceptance testing, etc.) will be kept up to date.

If there are multiple persons working on a site, we even create a new module for every setting group so the versions don't conflict when we merge code.

In your case, I would create a module that setups all the settings you want to keep. If you wipe the database, Magento will be completely clean AND including the settings you want!

You can have a look at the install/upgrade scripts of core Magento to see how Magento installs it's initial data/settings.

  • 2
    This is a good idea, there is only one problem: If you have installed extensions you might run into the problem, that they are not tested this way, so they throw errors, because the install scripts of the modules run before certain other modules and dependencies are not fulfilled. But I like this method too. – Fabian Blechschmidt May 14 '13 at 9:38
  • Can you explain that? If you are inserting a setting based on a third-party module in your own module, just declare the third-party module as a dependency (in your app/etc/module.xml) and your modules install/upgradescripts will run after the third-party modules'. – Erfan May 14 '13 at 10:08
  • 1
    yes, but if the third party module doesn't do that or inserts data in install instead of data scripts you might get problems because of the order. In most cases, the developers assume, that magento is already installed ;-) – Fabian Blechschmidt May 14 '13 at 12:04
  • 1
    One other potential problem is that you may want to maintain configuration settings and/or CMS content. So that would have to be backed up and re-imported after clearing the database entirely. – davidalger May 14 '13 at 14:21
  • 1
    @davidalger the point of these setup modules are to keep the configuration settings and/or CMS content so you dont have to re-import them. i.e. you can add CMS pages in your data-scripts so they get inserted again automatically after you cleared the db. – Erfan May 15 '13 at 9:05
7

If you don't need anything at all - just empty the whole DB and do a clean re-install.

Running mass-truncates always worry me with any potential lasting effects of broken key relationships.

Fast DB purging

Using this script,

cd /path/to/my/magento/store
wget -O mage-dbpurge.sh sys.sonassi.com/mage-dbpurge.sh
chmod +x mage-dbpurge.sh
./mage-dbpurge.sh

Are you 100% sure you want to purge $DBNAME? [y/N]: y
Are you 110% sure you want to purge $DBNAME? [y/N]: y

MYSQL DB PURGE COMPLETE

That will completely empty and remove every single table in the database. Do not use it if you need to retain anything.

  • Most of these really get sketchy. Starting with shutting off the foreign key dependencies. Isn't a relational database supposed to delete everything automatically if you for instance, delete the key product, key sales order, etc. while foreign key dependencies are active as all the records in all the referenced tables delete with the top level item? What if you miss a table name in that truncation list doing it with foreign key relationships shut off? The MagentoCommerce site is resplendent with oddball error messages that appear after these mass clears. – Fiasco Labs May 27 '13 at 14:36
  • Well, looking at his script, it will indeed drop every table. As for disabling foreign key checks, you need that so you don't have to figure out the proper order to drop all of the tables. – Lee Saferite May 28 '13 at 13:13
  • @LeeSaferite - I think that FiascoLabs was referring to the other solutions offering truncate-only type solutions versus the total obliteration I suggested. – Ben Lessani - Sonassi May 28 '13 at 17:10
  • Exactly what I meant! – Fiasco Labs May 29 '13 at 2:43
7

Prior to launching a new Magento eCommerce website, at development phase, you’ll always have to carry out testing and this will require creating test orders and customers to ensure that all processes function correctly and as you would expect.

This can often end up leaving a load of test data in the system which you won’t be wanting once the site is launched. Magento is notorious for inserting data in multiple database tables so we’ve provided the SQL script below that will cleanly delete all this data from your Magento database (and reset the increment counters for sales, invoices, customers and shipping). This has been tested and confirmed working on Magento versions from 1.4.0.1 to 1.9.0.1.

#Tested on Magento CE 1.4.1.1 - 1.9.0.1

SET FOREIGN_KEY_CHECKS=0;

##############################
# SALES RELATED TABLES
##############################
TRUNCATE `sales_flat_creditmemo`;
TRUNCATE `sales_flat_creditmemo_comment`;
TRUNCATE `sales_flat_creditmemo_grid`;
TRUNCATE `sales_flat_creditmemo_item`;
TRUNCATE `sales_flat_invoice`;
TRUNCATE `sales_flat_invoice_comment`;
TRUNCATE `sales_flat_invoice_grid`;
TRUNCATE `sales_flat_invoice_item`;
TRUNCATE `sales_flat_order`;
TRUNCATE `sales_flat_order_address`;
TRUNCATE `sales_flat_order_grid`;
TRUNCATE `sales_flat_order_item`;
TRUNCATE `sales_flat_order_payment`;
TRUNCATE `sales_flat_order_status_history`;
TRUNCATE `sales_flat_quote`;
TRUNCATE `sales_flat_quote_address`;
TRUNCATE `sales_flat_quote_address_item`;
TRUNCATE `sales_flat_quote_item`;
TRUNCATE `sales_flat_quote_item_option`;
TRUNCATE `sales_flat_quote_payment`;
TRUNCATE `sales_flat_quote_shipping_rate`;
TRUNCATE `sales_flat_shipment`;
TRUNCATE `sales_flat_shipment_comment`;
TRUNCATE `sales_flat_shipment_grid`;
TRUNCATE `sales_flat_shipment_item`;
TRUNCATE `sales_flat_shipment_track`;
TRUNCATE `sales_invoiced_aggregated`;            # ??
TRUNCATE `sales_invoiced_aggregated_order`;        # ??
TRUNCATE `log_quote`;

ALTER TABLE `sales_flat_creditmemo_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_shipping_rate` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_track` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated_order` AUTO_INCREMENT=1;
ALTER TABLE `log_quote` AUTO_INCREMENT=1;

#########################################
# DOWNLOADABLE PURCHASED
#########################################
TRUNCATE `downloadable_link_purchased`;
TRUNCATE `downloadable_link_purchased_item`;

ALTER TABLE `downloadable_link_purchased` AUTO_INCREMENT=1;
ALTER TABLE `downloadable_link_purchased_item` AUTO_INCREMENT=1;

#########################################
# RESET ID COUNTERS
#########################################
TRUNCATE `eav_entity_store`;
ALTER TABLE  `eav_entity_store` AUTO_INCREMENT=1;


##############################
# CUSTOMER RELATED TABLES
##############################
TRUNCATE `customer_address_entity`;
TRUNCATE `customer_address_entity_datetime`;
TRUNCATE `customer_address_entity_decimal`;
TRUNCATE `customer_address_entity_int`;
TRUNCATE `customer_address_entity_text`;
TRUNCATE `customer_address_entity_varchar`;
TRUNCATE `customer_entity`;
TRUNCATE `customer_entity_datetime`;
TRUNCATE `customer_entity_decimal`;
TRUNCATE `customer_entity_int`;
TRUNCATE `customer_entity_text`;
TRUNCATE `customer_entity_varchar`;
TRUNCATE `tag`;
TRUNCATE `tag_relation`;
TRUNCATE `tag_summary`;
TRUNCATE `tag_properties`;            ## CHECK ME
TRUNCATE `wishlist`;
TRUNCATE `log_customer`;

ALTER TABLE `customer_address_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `tag` AUTO_INCREMENT=1;
ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
ALTER TABLE `tag_properties` AUTO_INCREMENT=1;
ALTER TABLE `wishlist` AUTO_INCREMENT=1;
ALTER TABLE `log_customer` AUTO_INCREMENT=1;


##############################
# ADDITIONAL LOGS
##############################
TRUNCATE `log_url`;
TRUNCATE `log_url_info`;
TRUNCATE `log_visitor`;
TRUNCATE `log_visitor_info`;
TRUNCATE `report_event`;
TRUNCATE `report_viewed_product_index`;
TRUNCATE `sendfriend_log`;
### ??? TRUNCATE `log_summary`

ALTER TABLE `log_url` AUTO_INCREMENT=1;
ALTER TABLE `log_url_info` AUTO_INCREMENT=1;
ALTER TABLE `log_visitor` AUTO_INCREMENT=1;
ALTER TABLE `log_visitor_info` AUTO_INCREMENT=1;
ALTER TABLE `report_event` AUTO_INCREMENT=1;
ALTER TABLE `report_viewed_product_index` AUTO_INCREMENT=1;
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
### ??? ALTER TABLE `log_summary` AUTO_INCREMENT=1;

SET FOREIGN_KEY_CHECKS=1;
5

If you're working with EE you should also truncate the enterprise url rewrite tables, as they don't have foreign key constraints and on importing data (but not explicitly setting urls) the urls of the old products will be connected to your new imported products.

SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE `catalog_product_entity_url_key`;
TRUNCATE TABLE `catalog_category_entity_url_key`;
TRUNCATE TABLE `enterprise_url_rewrite`;
TRUNCATE TABLE `enterprise_url_rewrite_product_cl`;
TRUNCATE TABLE `enterprise_url_rewrite_category_cl`;
TRUNCATE TABLE `enterprise_catalog_category_rewrite`;
SET FOREIGN_KEY_CHECKS = 1;
4

One thing no one has mentioned is clearing tax tables. If you don't, you'll get repeat order Id entries in the tax table (when you place orders again at the starting Order Id, you'll have the same Id showing up multiple times). The multiple items will show up on invoices/memos as multiple line items, on both the admin and client side. After testing / clearing using the methods in the above posts (and not clearing the tax tables), after a while I had 10 different tax line items showing up for a single order.

Note - tax was/is still charged correctly, but on memos and invoices it was showing all of the items. Very confusing if a client was to see that.

So to add to the above:

TRUNCATE `sales_order_tax`;
TRUNCATE `sales_order_tax_item`;
ALTER TABLE `sales_order_tax` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_tax_item` AUTO_INCREMENT=1; 

And for good measure:

TRUNCATE `sales_payment_transaction`;
ALTER TABLE `sales_payment_transaction` AUTO_INCREMENT=1; 
2

You should have separate db for tests. See magento test framework. All ideas to clear db after tests are wrong.

2

I setup a repository for maintaining simple SQL scripts to run before launching a new Magento site. So far they can help:

  1. empty test data from Mage; orders, customers, reports, logs etc.
  2. empty test data from various extensions
  3. set desired IDs for next order, invoice, shipment and creditmemos

https://github.com/ccondrup/mage-reset
Help improve them!

  • Those scripts are great, however, is there a MySQL version they are supposed to run with? I tried on a server with a lower MySQL version than on my dev box and I got this: ERROR 1064 (42000) at line 160: 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 'DELIMITER ||DROP PROCEDURE IF EXISTS ccdata_empty_testdata|| This may be me, but otherwise great script!!! – Henry's Cat Dec 21 '15 at 14:06
  • @Henry'sCat Thanks - I guess if using a very old version of MySQL it might not support the DELIMITER command. I could not find which MySQL version it was introduced in. Try it in MySQL CLI, if it works there maybe it's just a matter of line endings in the script. – ccondrup Dec 27 '15 at 23:41
2

Just my 2 cents...

Rather than figuring out what to delete, and only realising what I have missed later, I run a few VMs, and I use snapshots at various stages, plus I have two separate sites - one live and one test.

It's easy then to have a script to "restore" test from live when I need it. Also, if the customer has access to the test system, they can play with it too before doing anything with live - no need to restore from a backup and losing orders and other changes in the process (though it does test your backup strategy...)

I think this a much better and "safer" process than running a custom (outdated?) script to delete (too much?) stuff...

(Also, feel free to downvote this as I can't comment yet!)

1

MYSQL Drop and create the DB then add the user privileges and magento admin user.

DROP DATABASE <dbname>;
CREATE DATABASE <dbname>;
GRANT ALL PRIVILEGES ON <dbname>.* TO <dbusr>@localhost IDENTIFIED BY '<dbusr_pass>';
FLUSH PRIVILEGES;

Add Magento admin user:

USE DB_NAME;
LOCK TABLES `admin_role` WRITE , `admin_user` WRITE;
SET @SALT = "rp";
SET @PASS = CONCAT(MD5(CONCAT( @SALT , "ADMIN_PASSWORD") ), CONCAT(":", @SALT ));
SELECT @EXTRA := MAX(extra) FROM admin_user WHERE extra IS NOT NULL;
INSERT INTO `admin_user` (firstname,lastname,email,username,password,created,lognum,reload_acl_flag,is_active,extra,rp_token_created_at) VALUES ('Firstname','Lastname','email@example.com','USER_NAME',@PASS,NOW(),0,0,1,@EXTRA,NOW());
INSERT INTO `admin_role` (parent_id,tree_level,sort_order,role_type,user_id,role_name) VALUES (1,2,0,'U',(SELECT user_id FROM admin_user WHERE username = 'USER_NAME'),'Firstname');
UNLOCK TABLES;
1

Based on an answer from Mufaddal I prepared query to clear order and related data in Magento 2.2

SET FOREIGN_KEY_CHECKS=0; 
TRUNCATE `sales_creditmemo`; 
TRUNCATE `sales_creditmemo_comment`; 
TRUNCATE `sales_creditmemo_grid`; 
TRUNCATE `sales_creditmemo_item`; 
TRUNCATE `sales_invoice`; 
TRUNCATE `sales_invoice_comment`; 
TRUNCATE `sales_invoice_grid`; 
TRUNCATE `sales_invoice_item`; 
TRUNCATE `sales_order`; 
TRUNCATE `sales_order_address`; 
TRUNCATE `sales_order_grid`; 
TRUNCATE `sales_order_item`; 
TRUNCATE `sales_order_payment`; 
TRUNCATE `sales_order_status_history`; 
TRUNCATE `quote`; 
TRUNCATE `quote_address`; 
TRUNCATE `quote_address_item`; 
TRUNCATE `quote_item`; 
TRUNCATE `quote_item_option`; 
TRUNCATE `quote_payment`; 
TRUNCATE `quote_shipping_rate`; 
TRUNCATE `sales_shipment`; 
TRUNCATE `sales_shipment_comment`; 
TRUNCATE `sales_shipment_grid`; 
TRUNCATE `sales_shipment_item`; 
TRUNCATE `sales_shipment_track`; 
TRUNCATE `sales_invoiced_aggregated`; 
TRUNCATE `sales_invoiced_aggregated_order`; 
TRUNCATE `sales_payment_transaction`;
TRUNCATE `sales_order_aggregated_created`; 
TRUNCATE `sales_order_tax`;
TRUNCATE `sales_order_tax_item`;
TRUNCATE `sendfriend_log`; 
TRUNCATE `cache_tag`;
-- TRUNCATE `tag`;
-- TRUNCATE `tag_relation`;
-- TRUNCATE `tag_summary`;
TRUNCATE `wishlist`; 
-- TRUNCATE `log_quote`;
-- TRUNCATE `report_event`;
ALTER TABLE `sales_creditmemo` AUTO_INCREMENT=1; 
ALTER TABLE `sales_creditmemo_comment` AUTO_INCREMENT=1; 
ALTER TABLE `sales_creditmemo_grid` AUTO_INCREMENT=1; 
ALTER TABLE `sales_creditmemo_item` AUTO_INCREMENT=1; 
ALTER TABLE `sales_invoice` AUTO_INCREMENT=1; 
ALTER TABLE `sales_invoice_comment` AUTO_INCREMENT=1; 
ALTER TABLE `sales_invoice_grid` AUTO_INCREMENT=1; 
ALTER TABLE `sales_invoice_item` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order_address` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order_grid` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order_item` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order_payment` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order_status_history` AUTO_INCREMENT=1; 
ALTER TABLE `quote` AUTO_INCREMENT=1; 
ALTER TABLE `quote_address` AUTO_INCREMENT=1; 
ALTER TABLE `quote_address_item` AUTO_INCREMENT=1; 
ALTER TABLE `quote_item` AUTO_INCREMENT=1; 
ALTER TABLE `quote_item_option` AUTO_INCREMENT=1; 
ALTER TABLE `quote_payment` AUTO_INCREMENT=1; 
ALTER TABLE `quote_shipping_rate` AUTO_INCREMENT=1; 
ALTER TABLE `sales_shipment` AUTO_INCREMENT=1; 
ALTER TABLE `sales_shipment_comment` AUTO_INCREMENT=1; 
ALTER TABLE `sales_shipment_grid` AUTO_INCREMENT=1; 
ALTER TABLE `sales_shipment_item` AUTO_INCREMENT=1; 
ALTER TABLE `sales_shipment_track` AUTO_INCREMENT=1; 
ALTER TABLE `sales_invoiced_aggregated` AUTO_INCREMENT=1; 
ALTER TABLE `sales_invoiced_aggregated_order` AUTO_INCREMENT=1; 
ALTER TABLE `sales_payment_transaction` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order_aggregated_created` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order_tax` AUTO_INCREMENT=1; 
ALTER TABLE `sales_order_tax_item` AUTO_INCREMENT=1; 
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1; 
-- ALTER TABLE `tag` AUTO_INCREMENT=1;
-- ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
-- ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
ALTER TABLE `cache_tag` AUTO_INCREMENT=1;
-- ALTER TABLE `wishlist` AUTO_INCREMENT=1;
-- ALTER TABLE `log_quote` AUTO_INCREMENT=1;
-- ALTER TABLE `report_event` AUTO_INCREMENT=1;
SET FOREIGN_KEY_CHECKS=1;
0

Simply got your database phpMyadmin and then open the table sales_flat_order and then simply remove tha data from this tabel and your test orders will be cleared. No need of any code ,commands or extensions . cool.

0

This is a late answer, but it could be helpful.

I'm developing an e-commerce website based on Magento 1.7 and I tend to TDD.

I think we should not bother by cleaning up database manually while we can use Magento beginTransaction and rollback methods.

Here is an example

public function setUp()
{
    parent::setUp();
    /** @var Mage_Core_Model_Resource $core */
    $core = Mage::getSingleton('core/resource');
    $core->getConnection('default_write')->beginTransaction();
}

public function tearDown()
{
    parent::tearDown();
    /** @var Mage_Core_Model_Resource $core */
    $core = Mage::getSingleton('core/resource');
    $core->getConnection('default_write')->rollBack();
    Mage::reset();
}

This answer is inspired by this Magento answer, visit it for further reading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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