How to Delete a WooCommerce Order Programmatically

In this tutorial I would actually show you how to really delete a WooCommerce order programmatically, I mean because when I tried to find info about it, I only found recommendations about using wp_delete_post() function (which we can’t use it since HPOS appeared) or woocommerce_delete_shop_order() function (there is no such function).

I needed it when I was developing a synced order deletion feature for my Multisite Order Sync plugin.

The long story short, let’s just dive straight into it.

Actually all we need to do to delete an order correctly is two things:

  1. Get a WC_Order object using wc_get_order() function.
  2. Use delete() method.

An example when you need to move an order to the trash:

$order = wc_get_order( $order_id );
$order->delete( false );

When you need to permanently delete an order:

$order = wc_get_order( $order_id );
$order->delete( true );

This method returns true if an order has been deleted, false – otherwise. Also be careful when using this method on a non-existing order, if( $order ) { condition will be helpful.

Deleting WooCommerce Orders via REST API

Well, technically it is also a programmatic way of deleting WooCommerce orders, so let’s take a look why not.

First of all let’s don’t forget that in order to interact with the WooCommerce REST API we can either use PHP or JS libraries or HTTP API. And I am going to show you the latter way.

wp_remote_request(
	'http://YOUR-WOO-STORE/wp-json/wc/v3/orders/' . $order_id,
	array(
		'method' => 'DELETE',
		'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "{$login}:{$pwd}" )
		),
	)
)

Don’t forget to replace $login and $pwd with a username and an application password.

If you want to skip the trash:

wp_remote_request(
	"http://YOUR-WOO-STORE/wp-json/wc/v3/orders/{$order_id}/?force=true",

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X