Skip to content

Instantly share code, notes, and snippets.

@martinboy
Created April 21, 2017 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinboy/cdac1b24946cd7b55e71b3c092019f36 to your computer and use it in GitHub Desktop.
Save martinboy/cdac1b24946cd7b55e71b3c092019f36 to your computer and use it in GitHub Desktop.
Magento 2 clear order's and customer's data
### QUOTE ###
DELETE FROM `quote`;
DELETE a1
FROM `quote_id_mask` a1
LEFT JOIN quote a2 ON a1.quote_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `quote_address` a1
LEFT JOIN quote a2 ON a1.quote_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `quote_item` a1
LEFT JOIN quote a2 ON a1.quote_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `quote_address_item` a1
LEFT JOIN quote_address a2 ON a1.quote_address_id = a2.address_id
LEFT JOIN quote_item a3 ON a1.quote_item_id = a3.item_id
WHERE a2.address_id IS NULL OR a3.item_id IS NULL;
DELETE a1
FROM `quote_item_option` a1
LEFT JOIN quote_item a2 ON a1.item_id = a2.item_id
WHERE a2.item_id IS NULL ;
DELETE a1
FROM `quote_payment` a1
LEFT JOIN quote a2 ON a1.quote_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `quote_shipping_rate` a1
LEFT JOIN quote_address a2 ON a1.address_id = a2.address_id
WHERE a2.address_id IS NULL ;
### CREDITMEMO ###
DELETE FROM `sales_creditmemo`;
DELETE a1
FROM `sales_creditmemo_comment` a1
LEFT JOIN sales_creditmemo a2 ON a1.parent_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_creditmemo_grid` a1
LEFT JOIN sales_creditmemo a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_creditmemo_item` a1
LEFT JOIN sales_creditmemo a2 ON a1.parent_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
### SHIPMENT ###
DELETE FROM `sales_shipment`;
DELETE a1
FROM `sales_shipment_track` a1
LEFT JOIN sales_shipment a2 ON a1.parent_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_shipment_item` a1
LEFT JOIN sales_shipment a2 ON a1.parent_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_shipment_grid` a1
LEFT JOIN sales_shipment a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_shipment_comment` a1
LEFT JOIN sales_shipment a2 ON a1.parent_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
### INVOICE ###
DELETE FROM `sales_invoice`;
DELETE a1
FROM `sales_invoice_comment` a1
LEFT JOIN sales_invoice a2 ON a1.parent_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_invoice_grid` a1
LEFT JOIN sales_invoice a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_invoice_item` a1
LEFT JOIN sales_invoice a2 ON a1.parent_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
### ORDER ###
DELETE FROM `sales_order`;
DELETE a1
FROM `sales_order_address` a1
LEFT JOIN sales_order a2 ON a1.parent_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_order_grid` a1
LEFT JOIN sales_order a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_order_item` a1
LEFT JOIN sales_order a2 ON a1.order_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_order_payment` a1
LEFT JOIN sales_order a2 ON a1.parent_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_order_status_history` a1
LEFT JOIN sales_order a2 ON a1.parent_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `sales_payment_transaction` a1
LEFT JOIN sales_order a2 ON a1.order_id = a2.entity_id
LEFT JOIN sales_order_payment a3 ON a1.payment_id = a3.entity_id
WHERE a2.entity_id IS NULL OR a3.entity_id IS NULL;
### CUSTOMERS ###
DELETE FROM `customer_entity`;
DELETE a1
FROM `customer_address_entity` a1
LEFT JOIN customer_entity a2 ON a1.parent_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `customer_address_entity_datetime` a1
LEFT JOIN customer_address_entity a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `customer_address_entity_decimal` a1
LEFT JOIN customer_address_entity a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `customer_address_entity_int` a1
LEFT JOIN customer_address_entity a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `customer_address_entity_text` a1
LEFT JOIN customer_address_entity a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `customer_address_entity_varchar` a1
LEFT JOIN customer_address_entity a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `customer_entity_datetime` a1
LEFT JOIN customer_entity a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `customer_entity_decimal` a1
LEFT JOIN customer_entity a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `customer_entity_int` a1
LEFT JOIN customer_entity a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `customer_entity_text` a1
LEFT JOIN customer_entity a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `customer_entity_varchar` a1
LEFT JOIN customer_entity a2 ON a1.entity_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
### WHISHLIST ###
DELETE a1
FROM `wishlist` a1
LEFT JOIN customer_entity a2 ON a1.customer_id = a2.entity_id
WHERE a2.entity_id IS NULL ;
DELETE a1
FROM `wishlist_item` a1
LEFT JOIN wishlist a2 ON a1.wishlist_id = a2.wishlist_id
WHERE a2.wishlist_id IS NULL ;
DELETE a1
FROM `wishlist_item_option` a1
LEFT JOIN wishlist_item a2 ON a1.wishlist_item_id = a2.wishlist_item_id
WHERE a2.wishlist_item_id IS NULL ;
@martinboy
Copy link
Author

The script is useful when you need to remove test orders.

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