Skip to content

Instantly share code, notes, and snippets.

@clivewalkden
Last active August 31, 2023 11:12
Show Gist options
  • Save clivewalkden/48648318cdec49e70ed24a4c159b836e to your computer and use it in GitHub Desktop.
Save clivewalkden/48648318cdec49e70ed24a4c159b836e to your computer and use it in GitHub Desktop.
Magento 2 Clear all tables after testing is complete.

Magento 2 Clear DB for Launch

Includes a basic clear.sql file for removing orders and customers and resetting the base invoice, order, shipment and creditmemo id's back to 0

A customweb_sagepay.sql for clearing out test SagePay orders from the customweb module.

A invoice_starting.sql for setting the start id's of invoices, orders, shipments and credit memos.

A mollie.sql for clearing out test orders using Mollie payment module.

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 `magento_operation`;
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_invoice_comment`;
TRUNCATE TABLE `sales_invoice_grid`;
TRUNCATE TABLE `sales_invoice_item`;
TRUNCATE TABLE `sales_invoiced_aggregated`;
TRUNCATE TABLE `sales_invoiced_aggregated_order`;
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_status_label`;
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`;
TRUNCATE TABLE `salesrule_coupon_aggregated`;
TRUNCATE TABLE `salesrule_coupon_aggregated_updated`;
TRUNCATE TABLE `salesrule_coupon_usage`;
TRUNCATE TABLE `salesrule_customer`;
TRUNCATE TABLE `tax_order_aggregated_created`;
TRUNCATE TABLE `tax_order_aggregated_updated`;
# 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`;
# Clean customer data
TRUNCATE TABLE `customer_address_entity`;
TRUNCATE TABLE `customer_address_entity_datetime`;
TRUNCATE TABLE `customer_address_entity_decimal`;
TRUNCATE TABLE `customer_address_entity_int`;
TRUNCATE TABLE `customer_address_entity_text`;
TRUNCATE TABLE `customer_address_entity_varchar`;
TRUNCATE TABLE `customer_entity`;
TRUNCATE TABLE `customer_entity_datetime`;
TRUNCATE TABLE `customer_entity_decimal`;
TRUNCATE TABLE `customer_entity_int`;
TRUNCATE TABLE `customer_entity_text`;
TRUNCATE TABLE `customer_entity_varchar`;
TRUNCATE TABLE `customer_grid_flat`;
TRUNCATE TABLE `customer_log`;
TRUNCATE TABLE `customer_visitor`;
TRUNCATE TABLE `jwt_auth_revoked`;
TRUNCATE TABLE `newsletter_subscriber`;
TRUNCATE TABLE `persistent_session`;
TRUNCATE TABLE `wishlist`;
TRUNCATE TABLE `wishlist_item`;
TRUNCATE TABLE `wishlist_item_option`;
# Reset search terms
TRUNCATE TABLE `catalogsearch_fulltext_scope1`;
TRUNCATE TABLE `search_query`;
# 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`;
# Misc tables
TRUNCATE TABLE `mageplaza_smtp_log`;
TRUNCATE TABLE `mailchimp_interest_group`;
TRUNCATE TABLE `mst_seo_audit_check_result`;
TRUNCATE TABLE `mst_seo_audit_job`;
TRUNCATE TABLE `mst_seo_audit_url`;
TRUNCATE TABLE `watchlog`;
TRUNCATE TABLE `amasty_conditions_quote`;
TRUNCATE TABLE `inventory_reservation`;
SET FOREIGN_KEY_CHECKS=1;
SET FOREIGN_KEY_CHECKS=0;
TRUNCATE TABLE `customweb_sagepaycw_customer_context`;
TRUNCATE TABLE `customweb_sagepaycw_external_checkout_context`;
TRUNCATE TABLE `customweb_sagepaycw_storage`;
TRUNCATE TABLE `customweb_sagepaycw_transaction`;
TRUNCATE TABLE `customweb_sagepaycw_transaction_grid`;
TRUNCATE TABLE `sequence_sagepaycw_transaction_1`;
SET FOREIGN_KEY_CHECKS=1;
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE sequence_invoice_1 AUTO_INCREMENT = 1006;
ALTER TABLE sequence_order_1 AUTO_INCREMENT = 1006;
ALTER TABLE sequence_shipment_1 AUTO_INCREMENT = 1006;
ALTER TABLE sequence_creditmemo_1 AUTO_INCREMENT = 1006;
SET FOREIGN_KEY_CHECKS=1;
SET FOREIGN_KEY_CHECKS=0;
TRUNCATE TABLE `mollie_order_lines`;
TRUNCATE TABLE `mollie_payment_customer`;
TRUNCATE TABLE `mollie_payment_paymenttoken`;
TRUNCATE TABLE `mollie_payment_transaction_to_order`;
TRUNCATE TABLE `vault_payment_token`;
TRUNCATE TABLE `vault_payment_token_order_payment_link`;
SET FOREIGN_KEY_CHECKS=1;
@clivewalkden
Copy link
Author

Added CustomWeb SagePay table cleanup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment