How do I programmatically delete an order? My head is about to pop. I'm trying to figure out how to delete commerce orders programmatically. I created a rest export with the order IDs I want to delete. I've parsed that and put the IDs into an array. Now I want to loop through that array and delete the orders.

json parsing, and retrieving order_id work fine.

I'm so close but I can't figure out how to properly reference commerce_order_delete or commerce_cart_commerce_order_delete?

function afc_custom_node_view(array &amp;$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
          if ($entity-&gt;id() == 803) {
            $url = 'my url';
            $jsonString = file_get_contents($url);
            $jsonDecoded = json_decode($jsonString, true);
            $object = new stdClass();
            foreach($jsonDecoded as $row){
            if ($row['order_id'] == 121) {
                $object-&gt;order_id = $row['order_id'];
                // $order = \Drupal::entityTypeManager()-&gt;getStorage('commerce_order')-&gt;load($row['order_id']);
                commerce_cart_commerce_order_delete($object);
            }

            }
          }
    }


Error:

TypeError: Argument 1 passed to commerce_cart_commerce_order_delete() must be an instance of Drupal\commerce_order\Entity\OrderInterface, instance of stdClass given,


UPDATE #2
New Code

use Drupal\commerce_order\Entity\OrderInterface;

  if ($entity-&gt;id() == 803) {
    $url = 'https://myurl.com/pull-ghosts';
    $jsonString = file_get_contents($url);
    $jsonDecoded = json_decode($jsonString, true);

    $object = new OrderInterface;

    foreach($jsonDecoded as $row){
      if ($row['order_id'] == 121) {
        $object-&gt;setOrderNumber($row['order_id']);
        commerce_cart_commerce_order_delete($object);
        print 'deleted';
      }
    }
  }


SAME ERROR:

error: Cannot instantiate interface Drupal\commerce_order\Entity\OrderInterface in afc_custom_node_view()
Loading it like a normal entity did the trick.

How to load and delete commerce order programmatically

$ord_id=121;
$order = \Drupal::entityTypeManager()-&gt;getStorage('commerce_order')-&gt;load($ord_id);
$order-&gt;delete();


Here is the entire snippet on my ultimate goal which was deleting abandoned orders in the cart.
How to remove commerce orders automatically

How to delete ghost commerce registrations in cart

Custom cron to delete commerce orders programmatically 

Load a view programmatically loop through values and delete orders

/**
 * Implements hook_cron().
 */
 function my_module_cron() {
   //load a view showing orders still in cart
   $view = \Drupal\views\Views::getView('commerce_carts');
   //the name of your display, i chose a simple rest display with the order_id field
   $view-&gt;setDisplay('rest_export_1');
   $view-&gt;execute();

   foreach($view-&gt;result as $row){
     //load the order
     $order = \Drupal::entityTypeManager()-&gt;getStorage('commerce_order')-&gt;load($row-&gt;order_id);
     $oid = $order-&gt;id();
     //delete the order
     $order-&gt;delete();
     //log to dblog
     \Drupal::logger('afc_commerce_registration')-&gt;notice('Clearing ghost registration, deleted: @oid.',
       array(
           '@oid' =&gt; $oid,
       ));
   }
 }


You can add the Created date field to the view, and test time values before deleting. In my case, it doesn't matter. 

Thanks for the help everyone