How to update cart after deleting line items? I have an Application (app) node that when it gets created (hook_node_insert) I add line items (based on info in the app) to my cart. This works great.

Step 2 now is that when I add a new app, I would like to empty the cart (or remove the line items) before adding the new line item (so there is effectively only ever 1 item in the cart).

If I try to do $item->delete() or $cart->delete() before I add the new line item (in my node insert hook) then I get this error:


  Drupal\Core\Entity\EntityStorageException: A cart order for type
  default, store 1 and account 1 already exists. in
  Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 847 of
  core\lib\Drupal\Core\Entity\Sql\SqlContentEntityStorage.php).


If I delete the line items after I add the new line item; then the cart almost looks correct. It shows only my new line item and the subtotal is the amount of my new line item; but the Total shown is the combined Total of the new line item added to the old line items (which have been successfully deleted).

My guess is there is something I need to do to "refresh" the cart after adding and then deleting line items?
instead of using $item->delete() this works:

  $cart_manager = \Drupal::service('commerce_cart.cart_manager');
  foreach($items as $item) {
    //$item-&gt;delete();
    $cart_manager-&gt;removeOrderItem($cart, $item, TRUE);
  }


the 3rd parameter is 'save cart' and setting it to TRUE updates the cart Total.