Delete all failed orders programmatically in WooCommerce

/**
 * Deletes all failed orders.
 *
 * To move orders to the trash instead of deleting them permanently:
 * `$order->delete( false );` instead of `$order->delete( true );`
 *
 * @see https://wpcodebook.com/woocommerce-delete-failed-orders-programmatically/
 * @see https://woocommerce.github.io/code-reference/classes/WC-Order.html
 * @see https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query
 */

$orders = wc_get_orders( array(
	'status' => 'failed',
	'limit'  => -1,
) );

foreach ( $orders as $order ) {
	$order->delete( true );
}

Leave a Comment