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);

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!