Magento 2: SQL Queries to Delete Customer Data
This article gives you organized SQL queries for deleting customer-related data in Magento 2.
Important: customer data in Magento can mean two different scopes:
1. Customer accounts only — customers, addresses, grids, wishlist, compare, login/session style data.
2. Full customer footprint — everything above plus quotes, orders, invoices, shipments, credit memos, reviews, newsletter, and reward/store-credit style extension data if present.
The safest default is to start with customer accounts only.
Option 1: Delete customer accounts only
SET FOREIGN_KEY_CHECKS = 0; 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_address_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_entity; TRUNCATE TABLE customer_grid_flat; TRUNCATE TABLE wishlist_item_option; TRUNCATE TABLE wishlist_item; TRUNCATE TABLE wishlist; TRUNCATE TABLE catalog_compare_item; TRUNCATE TABLE persistent_session; SET FOREIGN_KEY_CHECKS = 1;
Option 2: Delete customer accounts plus quotes and carts
SET FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE quote_address_item; TRUNCATE TABLE quote_item_option; TRUNCATE TABLE quote_item; TRUNCATE TABLE quote_payment; TRUNCATE TABLE quote_shipping_rate; TRUNCATE TABLE quote_id_mask; TRUNCATE TABLE quote_address; TRUNCATE TABLE quote; 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_address_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_entity; TRUNCATE TABLE customer_grid_flat; TRUNCATE TABLE wishlist_item_option; TRUNCATE TABLE wishlist_item; TRUNCATE TABLE wishlist; TRUNCATE TABLE catalog_compare_item; TRUNCATE TABLE persistent_session; SET FOREIGN_KEY_CHECKS = 1;
Option 3: Delete customer accounts plus sales history
SET FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE sales_creditmemo_comment; TRUNCATE TABLE sales_creditmemo_grid; TRUNCATE TABLE sales_creditmemo_item; TRUNCATE TABLE sales_creditmemo; TRUNCATE TABLE sales_invoice_comment; TRUNCATE TABLE sales_invoice_grid; TRUNCATE TABLE sales_invoice_item; TRUNCATE TABLE sales_invoice; TRUNCATE TABLE sales_shipment_comment; TRUNCATE TABLE sales_shipment_grid; TRUNCATE TABLE sales_shipment_item; TRUNCATE TABLE sales_shipment_track; TRUNCATE TABLE sales_shipment; TRUNCATE TABLE sales_order_address; 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_item; TRUNCATE TABLE sales_order; SET FOREIGN_KEY_CHECKS = 1;
Option 4: Delete everything customer-related including accounts, quotes, and sales
SET FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE sales_creditmemo_comment; TRUNCATE TABLE sales_creditmemo_grid; TRUNCATE TABLE sales_creditmemo_item; TRUNCATE TABLE sales_creditmemo; TRUNCATE TABLE sales_invoice_comment; TRUNCATE TABLE sales_invoice_grid; TRUNCATE TABLE sales_invoice_item; TRUNCATE TABLE sales_invoice; TRUNCATE TABLE sales_shipment_comment; TRUNCATE TABLE sales_shipment_grid; TRUNCATE TABLE sales_shipment_item; TRUNCATE TABLE sales_shipment_track; TRUNCATE TABLE sales_shipment; TRUNCATE TABLE sales_order_address; 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_item; TRUNCATE TABLE sales_order; TRUNCATE TABLE quote_address_item; TRUNCATE TABLE quote_item_option; TRUNCATE TABLE quote_item; TRUNCATE TABLE quote_payment; TRUNCATE TABLE quote_shipping_rate; TRUNCATE TABLE quote_id_mask; TRUNCATE TABLE quote_address; TRUNCATE TABLE quote; 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_address_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_entity; TRUNCATE TABLE customer_grid_flat; TRUNCATE TABLE wishlist_item_option; TRUNCATE TABLE wishlist_item; TRUNCATE TABLE wishlist; TRUNCATE TABLE catalog_compare_item; TRUNCATE TABLE persistent_session; SET FOREIGN_KEY_CHECKS = 1;
Optional extras if you also want to remove newsletter subscriptions
TRUNCATE TABLE newsletter_subscriber;
Optional extras if you also want to remove reviews written by customers
SET FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE rating_option_vote; TRUNCATE TABLE review_detail; TRUNCATE TABLE review_store; TRUNCATE TABLE review_entity_summary; TRUNCATE TABLE review; SET FOREIGN_KEY_CHECKS = 1;
Optional cleanup for sequence tables
TRUNCATE TABLE sequence_order_0; TRUNCATE TABLE sequence_invoice_0; TRUNCATE TABLE sequence_shipment_0; TRUNCATE TABLE sequence_creditmemo_0;
Reindex and clear cache after cleanup
bin/magento indexer:reindex bin/magento cache:flush