I recently had to clean out orders via sql which seems to be a pretty substantial query for enterprise. Not really a big deal, but the client I'm working with is wondering about adding a delete option for pending orders that would remove just that order.
Are there details on how to programmatically remove a single order and associated data for Magento 2.2 EE available?
Hello @joseph_digiovanna,
You can use the following code to delete the order in the external file and put it magento root directory
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); require 'app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); $app = $bootstrap->createApplication('Magento\Framework\App\Http'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $objectManager->get('Magento\Framework\App\State')->setAreaCode('frontend'); // adminhtml $registry = $objectManager->get('Magento\Framework\Registry'); $id = 10; // your order_id $order = $objectManager->create('Magento\Sales\Model\Order')->load($id); // $incrementId = 'xxxxxxxxx'; // $order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($incrementId); $registry->register('isSecureArea','true'); $order->delete(); $registry->unregister('isSecureArea');
--
If my answer is useful, please give Kudos or Accept as Solution
Hello joseph_digiovanna
Please find below free order delete extension link for delete single order.
Url : https://www.mageplaza.com/magento-2-delete-orders/
Url : https://www.wyomind.com/magento2/order-eraser-magento.html
Truncate order tables by query
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;
Hope it will help to solve the problem then please accept as solution and kudos.
Hi,
it deletes only the order history, how to remove creditmemo,shipments,invoices..etc
Hello @nikhil_velaga ,
You can clear creditmemo, shipment and invoice history by using above SQL query https://community.magento.com/t5/Magento-2-x-Programming/Programmatically-deleting-an-order-and-asso...
--
If my answer is useful, please Accept as Solution & give Kudos
Make sure to remove all invoices,shipments,creditmemos of the order first. Then you can delete the order.
$incrementId = 10000342; $order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($incrementId); //or //$order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId); $invoices = $order->getInvoiceCollection(); foreach ($invoices as $invoice) { $invoice->delete(); } $shipments = $order->getShipmentsCollection(); foreach ($shipments as $shipment) { $shipment->delete(); } $creditmemos = $order->getCreditmemosCollection(); foreach ($creditmemos as $creditmemo) { $creditmemo->delete(); } // delete invoices/shipments/creditmemos & then finally delete order $order->delete();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$id = 10;// your order_id
$order = $objectManager->create('Magento\Sales\Model\Order')->load($id);
// $incrementId = 'xxxxxxxxx'; //
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($incrementId); $order->delete();
Hi, Please use the below code to clean up the order.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $incrementId = "2000000008"; $orderInterface = $objectManager->create('Magento\Sales\Api\Data\OrderInterface'); $order = $orderInterface->loadByIncrementId($incrementId); $orderId = $order->getId(); if($orderId) { $invoices = $order->getInvoiceCollection(); if(count($invoices->getData())) { foreach ($invoices as $invoice){ $items = $invoice->getAllItems(); foreach ($items as $item) { $item->delete(); } $invoice->delete(); } } $creditnotes = $order->getCreditmemosCollection(); if(count($creditnotes->getData())) { foreach ($creditnotes as $creditnote){ $items = $creditnote->getAllItems(); foreach ($items as $item) { $item->delete(); } $creditnote->delete(); } } $shipments = $order->getShipmentsCollection(); if(count($shipments->getData())) { foreach ($shipments as $shipment){ $items = $shipment->getAllItems(); foreach ($items as $item) { $item->delete(); } $shipment->delete(); } } $order->delete(); }