Kaskus

Tech

daniel8115Avatar border
TS
daniel8115
How to delete a test orders in Magento 2?
When you test a new module, you need to create many test orders in order to ensure that the module works without any troubles before enabling for customers. Magento 2 Delete Order extension the perfect solution to delete all test orders which can make you confused.

Clearing all orders, shipments, transactions etc.:
Code:
[color=#393318][size=2][font=Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif][php]SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE `sales_order`;
TRUNCATE `sendfriend_log`;
TRUNCATE `wishlist`;
TRUNCATE `report_event`;

ALTER TABLE `sales_order` AUTO_INCREMENT=1;
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
ALTER TABLE `wishlist` AUTO_INCREMENT=1;
ALTER TABLE `report_event` AUTO_INCREMENT=1;[/php][/font][/size][/color]

Tested in Magento 2.1.0
Be safe : make a backup of your sql first.
Use the sql below according to your needs :
Code:
[color=#242729][size=2][font=Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif][php]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;


SET FOREIGN_KEY_CHECKS=1;
[/php][/font][/size][/color]

DO NOT TRUNCATE / EMPTY THE FOLLOWING :
  • sales_order_status
  • sales_sequence_meta
  • sales_sequence_profile
  • sales_order_status_label
  • sales_order_status_state

You can delete order via programmatically:
Code:
[color=#242729][size=2][font=Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif][php]$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$order = $objectManager->create('Magento\Sales\Model\Order')->getCollection()

->addFieldToFilter('entity_id', (array) $orderIds);

foreach ($orders as $o) {

//load order object - I know it's not ok to use load in a loop but it

should be ok since it's a one time script

$order = Mage::getModel('sales/order')->load($o->getId());

//delete all order items

$items = $order->getAllItems();

foreach ($items as $item) {

   $item->delete();

}

$invoices = $order->getInvoiceCollection();

foreach ($invoices as $invoice){

//delete all invoice items

   $items = $invoice->getAllItems();

   foreach ($items as $item) {

       $item->delete();

   }

   //delete invoice
   $invoice->delete();
}

$creditnotes = $order->getCreditmemosCollection();
foreach ($creditnotes as $creditnote){

 //delete all creditnote items

   $items = $creditnote->getAllItems();
   foreach ($items as $item) {
       $item->delete();
   }
   //delete credit note
   $creditnote->delete();
}
$shipments = $order->getShipmentsCollection();
foreach ($shipments as $shipment){

 //delete all shipment items

   $items = $shipment->getAllItems();
   foreach ($items as $item) {
       $item->delete();
   }
   //delete shipment
   $shipment->delete();
}[/php][/font][/size][/color]


Or you can make it become easy by in use this simple extension available for FREE for Magento 2 here: -> https://www.mageplaza.com/magento-2-delete-orders/

or download FREE Delete orders for Magento 2 on GitHub
Diubah oleh daniel8115 12-07-2018 10:24
0
999
0
GuestAvatar border
Komentar yang asik ya
Mari bergabung, dapatkan informasi dan teman baru!
Website, Webmaster, Webdeveloper
Website, Webmaster, Webdeveloper
KASKUS Official
23.5KThread5.3KAnggota
GuestAvatar border
Komentar yang asik ya
Ikuti KASKUS di
© 2025 KASKUS, PT Darta Media Indonesia. All rights reserved.