In this post, we’ll walk you through all the effective methods to remove items from the cart in Magento 2, complete with clear explanations and working code examples.
Managing cart items is a fundamental part of any eCommerce experience. Magento 2 provides multiple ways to remove products from cart, whether through the frontend UI, programmatically, or via APIs(Application programming interface).
1. Delete Cart Item from Frontend (Default Way)
Magento 2 already provides a “Remove Item” option in the mini cart and cart page.
How it works:
- Each cart item has a remove (×) icon
- Clicking it triggers an AJAX request
- Magento internally calls
\Magento\Checkout\Controller\Cart\Delete
This is the recommended approach for standard stores, as it follows Magento’s default flow and handles session, totals, and events automatically.
2. Remove Item from Cart Programmatically (By Item ID)
If you are developing a custom module, you may need to remove a cart item using PHP.
Example: Remove cart item using Quote Item ID
use Magento\Checkout\Model\Cart;
public function removeItemFromCart($itemId)
{
$this->cart->removeItem($itemId)->save();
}
Key points:
$itemIdis the quote_item_id, not the product IDsave()is required to persist changes- Totals are recalculated automatically
3. Remove Cart Item Using Product ID
Sometimes you only have the product ID, not the cart item ID.
Example:
$quote = $this->cart->getQuote();
foreach ($quote->getAllItems() as $item) {
if ($item->getProductId() == $productId) {
$quote->removeItem($item->getId());
}
}
$quote->save();
Use cases:
- Auto-removing promotional products
- Conditional cart cleanup logic
- Custom checkout rules
4. Delete All Items from Cart (Empty Cart)
Magento 2 allows clearing the entire cart programmatically.
Example:
$this->cart->truncate()->save();
When to use:
- “Clear Cart” button
- After logout
- After order placement for custom flows
5. Remove Cart Item Using AJAX (Custom Button)
If you want a custom Remove button, you can create an AJAX controller.
Controller Example:
namespace Vendor\Module\Controller\Cart;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Checkout\Model\Cart;
class Remove extends Action
{
protected $cart;
public function __construct(Context $context, Cart $cart)
{
parent::__construct($context);
$this->cart = $cart;
}
public function execute()
{
$itemId = (int)$this->getRequest()->getParam('item_id');
$this->cart->removeItem($itemId)->save();
}
}
6. Remove Cart Item Using REST API
Magento 2 also supports cart item deletion via API.
Guest Cart:
DELETE /rest/V1/guest-carts/{cartId}/items/{itemId}
Customer Cart:
DELETE /rest/V1/carts/mine/items/{itemId}
Common use cases:
- Mobile apps
- Headless storefronts
- Third-party integrations
7. Important Things to Remember
- Item ID ≠ Product ID
- Always call
save()after cart modification - Use
truncate()only when clearing the full cart - Prefer Magento’s default controllers for frontend actions
- Recalculate totals if you modify quote directly
8. Best Practices
✔ Use the default Magento cart delete wherever possible
✔ Avoid direct DB operations on quote_item
✔ Use events/observers for conditional removal
✔ Test for guest and logged-in customers
I hope this blog will help you with how to Delete Items from the cart in Magento 2. You may also check our wide range of best Magento 2 Extensions.
Please reach out to our team via a support ticket if you have any queries.
Try this, and if you have any queries, then just comment below 🙂
I remove the item as your guide, however, the mini-cart is not updated. Can you please kindly advise what I can do? Thank you so much.