Magento 2: Delete sample data and test data from your store

Here is a script to delete all the test data from your magento store. This can come in handy especially at the time of launching your website from beta to production mode. Don't forget to bookmark it for your next launch.

NOTE: It is advisable to take full database backup before performing any operations below:

SCRIPT:
SET FOREIGN_KEY_CHECKS=0;
# Clean order history
TRUNCATE TABLE `sales_bestsellers_aggregated_daily`;
TRUNCATE TABLE `sales_bestsellers_aggregated_monthly`;
TRUNCATE TABLE `sales_bestsellers_aggregated_yearly`;

# Clean order infos
TRUNCATE TABLE `sales_creditmemo`;
TRUNCATE TABLE `sales_creditmemo_comment`;
TRUNCATE TABLE `sales_creditmemo_grid`;
TRUNCATE TABLE `sales_creditmemo_item`;
TRUNCATE TABLE `sales_invoice`;
TRUNCATE TABLE `sales_invoiced_aggregated`;
TRUNCATE TABLE `sales_invoiced_aggregated_order`;
TRUNCATE TABLE `sales_invoice_comment`;
TRUNCATE TABLE `sales_invoice_grid`;
TRUNCATE TABLE `sales_invoice_item`;
TRUNCATE TABLE `sales_order`;
TRUNCATE TABLE `sales_order_address`;
TRUNCATE TABLE `sales_order_aggregated_created`;
TRUNCATE TABLE `sales_order_aggregated_updated`;
TRUNCATE TABLE `sales_order_grid`;
TRUNCATE TABLE `sales_order_item`;
TRUNCATE TABLE `sales_order_payment`;
TRUNCATE TABLE `sales_order_status_history`;
TRUNCATE TABLE `sales_order_tax`;
TRUNCATE TABLE `sales_order_tax_item`;
TRUNCATE TABLE `sales_payment_transaction`;
TRUNCATE TABLE `sales_refunded_aggregated`;
TRUNCATE TABLE `sales_refunded_aggregated_order`;
TRUNCATE TABLE `sales_shipment`;
TRUNCATE TABLE `sales_shipment_comment`;
TRUNCATE TABLE `sales_shipment_grid`;
TRUNCATE TABLE `sales_shipment_item`;
TRUNCATE TABLE `sales_shipment_track`;
TRUNCATE TABLE `sales_shipping_aggregated`;
TRUNCATE TABLE `sales_shipping_aggregated_order`;

# Clean cart infos
TRUNCATE TABLE `quote`;
TRUNCATE TABLE `quote_address`;
TRUNCATE TABLE `quote_address_item`;
TRUNCATE TABLE `quote_id_mask`;
TRUNCATE TABLE `quote_item`;
TRUNCATE TABLE `quote_item_option`;
TRUNCATE TABLE `quote_payment`;
TRUNCATE TABLE `quote_shipping_rate`;

# Reset indexes (if you want your orders number start back to 1
TRUNCATE TABLE sequence_invoice_1;
TRUNCATE TABLE sequence_order_1;
TRUNCATE TABLE sequence_shipment_1;
TRUNCATE TABLE sequence_creditmemo_1;
#Do this for every store ID as suffix

#Return / RMA
TRUNCATE TABLE magento_rma;
TRUNCATE TABLE magento_rma_item_entity;
TRUNCATE TABLE magento_rma_shipping_label;

TRUNCATE TABLE magento_rma_grid;
TRUNCATE TABLE sequence_rma_item_0;
TRUNCATE TABLE sequence_rma_item_1;
#Do this for every store ID as suffix
SET FOREIGN_KEY_CHECKS=1;
DO NOT TRUNCATE / EMPTY THE FOLLOWING :
  • sales_order_status
  • sales_sequence_meta
  • sales_sequence_profile
  • sales_order_status_label
  • sales_order_status_state
If you want to go a step further and delete your catalog, here is a good reference point to get you started: https://gist.github.com/leek/f7fd2c41668b0f8a0a5f6e5209fe8f4e

Comments

Popular posts from this blog

Magento 2: How to create new indexer for your custom module

Xdebug - PHPStorm - Ubuntu: How to configure Xdebug on Ubuntu for PHPStorm

Magento 2 - Vue Storefront PWA Integration: How to create a new VSBridge indexer job and Elastic Index for your custom module