1

How can we delete a Magento2 order programatically? There's ways to do this in Magento 1.X. Is it the same methodology for Magento2 as well?

3

5 Answers 5

2

You should use \Magento\Sales\Api\Data\OrderInterface.

/** @var \Magento\Sales\Api\Data\OrderInterface $order **/

protected $order;

public function __construct(
    \Magento\Sales\Api\Data\OrderInterface $order,
    ......
) {
    $this->order = $order;

}

Load order object by increment id and delete it.

$this->order->loadByIncrementId('XXXXXXXXX');
$this->order->delete();
3
  • Does this delete too related entities, such as invoice, shipment, etc...? Oct 1, 2019 at 9:06
  • @RaulSanchez Yes. But first you can check in development environment. Oct 1, 2019 at 9:15
  • how to archive order programmatically? Jun 29, 2020 at 13:06
2

You can use following code to delete the order.

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$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'); 

Note: I used the objectManager for the demonstration. Always use dependency injection.

3
  • Does this delete too related entities, such as invoice, shipment, etc...? Oct 1, 2019 at 9:07
  • I have the same doubt of @RaulSanchez this delete the order items and all the other entities? May 22, 2020 at 14:55
  • can you please let me know how to archive the order from the script. Jun 29, 2020 at 13:05
1

Method #1: SQL queries

Try to run the following 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;
```

Method #2 M2 source code:

 protected function massAction(DbAC $collection)
    {
        if ($this->helper->isEnabled()) {
            $orderDeleted = 0;
            foreach ($collection as $order) {
                $this->orderRepository->deleteById($order->getId());
                $orderDeleted++;
            }
            if ($orderDeleted) {
                $this->messageManager->addSuccess(__('A total of %1 record(s) were deleted.', $orderDeleted));
            }
        } else {
            $this->messageManager->addError(__('Delete Order module is Disabled'));
        }
        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setPath($this->getComponentRefererUrl());
        return $resultRedirect;
    }

See controller: https://github.com/mageplaza/magento-2-delete-orders/blob/master/Controller/Adminhtml/Order/Delete.php

Learn more https://github.com/mageplaza/magento-2-delete-orders .

2
  • Does source code method delete too related entities, such as invoice, shipment, etc...? Oct 1, 2019 at 9:07
  • Yes sure. Delete related billing documents
    – Say Truth
    Jun 2, 2020 at 3:23
0

Delete a range of items, can be done in n98-magerun2 dev:console

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$registery = $objectManager->get('Magento\Framework\Registry');

foreach(range(4000000010, 4000000100) as $id) {
    $order = $objectManager->create( 'Magento\Sales\Model\Order' )->loadByIncrementId( $id );
    $registery->register( 'isSecureArea', 'true' );
    $order->delete();
    $registery->unregister( 'isSecureArea' );
}
1
  • Does this delete too related entities, such as invoice, shipment, etc...? Oct 1, 2019 at 9:06
-1

Truncate order tables

TRUNCATE TABLE `gift_message`;
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`;
TRUNCATE TABLE `reporting_orders`;
TRUNCATE TABLE `sales_bestsellers_aggregated_daily`;
TRUNCATE TABLE `sales_bestsellers_aggregated_monthly`;
TRUNCATE TABLE `sales_bestsellers_aggregated_yearly`;
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`;
TRUNCATE TABLE `tax_order_aggregated_created`;
TRUNCATE TABLE `tax_order_aggregated_updated`;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.