Site icon Qaisar Satti's Blogs

How to Delete a WooCommerce Order Programmatically

In this tutorial, we will teach you how to delete a WooCommerce order programmatically today. There are 2 methods that you can use to delete the order. One is moving the order to the trash and the other is deleting the order permanently. You can either use wp_delete_post() function or you can use wc_get_order() then delete() function.  For HPOS use wc_get_order() then delete() function.

Delete order with HPOS

When you want to move the order to the trash : 

$orderId = 10;
$orderDetail = wc_get_order( $orderId )
$orderDetail->delete( false );

To delete an order permanently:

$orderId = 10;
$orderDetail = wc_get_order( $orderId )
$orderDetail->delete( true );

Delete order with wp_delete_post()

When you want to move the order to the trash : 

$orderId = 10;
wp_delete_post($orderId, false);

To delete an order permanently:

$orderId = 10;
wp_delete_post($orderId, true);
Exit mobile version