Magento GuideCart → Empty

How to empty a Magento 2 cart using JS

Emptying a customer’s cart with a button click can improve usability and help support advanced shopping flows. If you’re building a custom frontend experience or integrating Magento with a JS-heavy theme, you’ll need to handle this without a full page reload.

Let’s walk through how to clear a Magento 2 cart using JavaScript and a custom controller.

Host Magento at full throttle.

Get secure, reliable Magento hosting so you can scale faster.

Why clear the cart with JavaScript?

JavaScript gives you more control over the customer experience when clearing the cart:

The key is to build a custom endpoint that safely empties the cart, then call it with JavaScript.

1. Create a custom controller to clear the cart

First, create a custom module with a controller that clears the current cart session. You’ll use the Magento\Checkout\Model\Cart class to truncate the quote and save it.

Here’s the full controller code:

Make sure to register your module and add the correct routes.xml entry to expose this controller at a URL like /vendor/module/cart/clear.

2. Add a JavaScript function to call the controller via AJAX

Once your controller is in place, you can call it from the frontend using JavaScript. Here’s an example using jQuery’s $.ajax:

Make sure window.FORM_KEY is available on the page. It’s a required CSRF token for any POST request in Magento.

3. Trigger the function with a button or event

To use the function, hook it into a button click or another user event:

You can place this in a PHTML template or insert it into the DOM dynamically. If you’re building with RequireJS, you may also need to declare jQuery as a dependency in your module’s requirejs-config.js.

In a Knockout-based UI (like the mini-cart), you could also bind this to an observable or UI component.

4. Refresh cart and mini-cart UI after clearing

Clearing the cart won’t automatically update the mini-cart or cart sidebar. After calling the AJAX controller, you should refresh the UI so the customer sees the cart as empty.

The fastest way is to reload the page:

For a smoother experience, especially in headless or PWA setups, reload just the cart data:

This updates Magento’s customer data sections, including the mini-cart, without a full reload.

5. Key security and performance considerations

Don’t skip these technical best practices:

6. Troubleshooting tips

If something’s not working, check the following:

7. When to use this method (and when not to)

This approach is flexible but not ideal for every situation.

Use it when:

Avoid it when:

Next steps for emptying a Magento 2 cart with JS

Using JavaScript and a custom controller gives you a fast, flexible way to clear Magento 2 carts. It’s perfect for user-friendly features, testing tools, and custom shopping flows.

To get started, set up your custom controller, wire up the frontend AJAX call, and test it thoroughly in your theme.

Ready to upgrade your Magento experience? Professional hosting improves speeds, security, and reliability for a website and a brand that people find engaging and trustworthy.

Liquid Web offers the raw infrastructure power you need with mission-critical features that keep your store running smoothly. Most importantly, our in-house Magento experts are standing by to help with both hosting and Magento application roadblocks.

Click through below to explore all of our Magento hosting options, or chat with an expert right now to get answers and advice.

Ready to get started?

Get the fastest, most secure Magento hosting on the market

Additional resources

What is Magento Ecommerce? →

A complete beginner’s guide to the Magento Ecommerce platform

Magento shopping cart: How to setup, optimize, and manage →

Let’s walk through everything you need to know about configuring, optimizing, and maintaining your Magento cart the right way.

Best Magento ERP extensions →

Our top 10 compared so you can decide which is best for your business