Random 400 errors on line item removal

Short description of issue

Random 400 errors on line item removal

Reproduction steps

Add items to the cart
Remove item from the cart

Additional info

In my cart, I remove items by using the cart/change function and I remove them by their line item key, so I always know it’s the right variant being removed, but very randomly I am getting a 400 error when removing, meaning the object passed is not valid. The error itself says “not a valid id or key” but it works the other half of the time without any changes in the elements/code/etc.

What could possibly be the culprit here?

What type of topic is this

Troubleshooting

Upload screenshot(s) of issue

Hey @yvony - thanks for sharing the screenshot, that’s helpful.

This does look a bit odd, but the error shown there looks like the localized version of the change.js 400 response for “no valid id or line parameter”, which generally means the line item couldn’t be identified in the current cart at the time Shopify processed the request.

One thing I’d check here is whether the line item key in your DOM is occasionally stale. Line item keys can change when line item characteristics change, including properties or discount applications, so a key that was valid when the cart drawer rendered may not always still be valid by the time the remove request is made.

Could you share a bit more detail here?

  • The exact payload you’re sending to /cart/change.js when the 400 happens
  • Whether multiple cart requests can run at the same time, or whether the remove button is disabled while a request is in flight
  • Whether the affected cart has automatic discounts, line item properties, selling plans, subscription apps, or other cart-modifying logic
  • If possible, a /cart.js snapshot immediately before the failing remove request, specifically the current items[].key values

A useful test would be to fetch the latest /cart.js right before removal and confirm whether the key you’re about to send still exists in cart.items. If it doesn’t, that would point pretty strongly to a stale cart render or concurrent cart mutation rather than the key format itself.

Hope to hear from you soon!

Hey Alan,

I definitely checked the current line key to make sure the correct item was added to the object to be removed, but it still happened. So a stale cart was not the cause. There are also no multiple cart requests, no discounts, properties or selling plans. It’s still a dev environment and just a normal add to cart/remove to cart action.

I’ve changed the logic to an update call instead of an change call and that seems to work, but I do still want to know why it wouldn’t work.

Cheers,
Yvonne

So I found the issue. The custom element that was triggering the remove had an A element in it with a link to {{ item.url_to_remove }}. For some reason this was never an issue and the event was prevented, but this stopped working. :woman_shrugging: I’ve removed the link and now it works without errors…