Table of contents
Get the industry’s most secure Magento hosting◦ 99.99% uptime
◦ Comprehensive security
◦ 24/7 support

Magento GuideCart → Clear

How to clear a Magento 2 cart with a URL (programmatically)

Clearing a shopping cart in Magento 2 can be frustrating if you want to do it automatically—like via a link or button—but there’s no built-in URL for it. The good news? You can create one yourself. With just a few lines of PHP and a custom module, you can programmatically clear a cart in Magento 2 using a direct URL.

Let’s walk through everything you need to build this step by step—even if you’re new to Magento development.

Host Magento at full throttle.

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

How cart clearing with a URL works in Magento 2

Magento 2 doesn’t offer a default route like clearcart=true that you can just add to the end of a URL. Instead, you’ll need to build this functionality yourself using Magento’s modular architecture.

To clear a Magento cart via URL, you’ll create:

Once set up, any visit to your custom URL will instantly clear the cart.

Step 1: Create your custom module

All custom functionality in Magento lives inside a module. To start, create the folder structure for your module in app/code.

This step registers your new module and makes it active in Magento 2.

Step 2: Add a custom route

Now that the module is in place, you need a route so Magento knows which URL should trigger the cart-clearing action.

Create a routes.xml file inside app/code/Vendor/CartClear/etc/frontend/:

This makes yourstore.com/clearcart/index/index available to use.

Step 3: Create the controller to empty the cart

Now you’ll create the PHP class that will run when someone visits your custom URL. This class will use Magento’s cart model to clear the cart contents.

What’s happening here:

You can now visit:

https://yourstore.com/clearcart/index/index

…and the cart will instantly be cleared.

Alternative method: loop through and remove items

If you don’t want to clear everything or need more control—for example, only removing certain SKUs—you can loop through cart items manually.

Replace the execute() method with this version:

This approach is useful if you want to:

Testing and security tips

While it’s convenient to have a URL that clears the cart, this opens up security considerations.

Make sure to:

Magento 2 cart clearing FAQs

Manually, you can remove items one by one from the cart. Programmatically, you can create a custom controller that uses Magento’s truncate() method to instantly clear all items in the cart for a user.

You can clear specific cache types in code:

Or use CLI:

Static files are generated during deployment. To clear and regenerate them:

This ensures your frontend files (like JS and CSS) are refreshed.

Inject the CollectionFactory in your class like this:

You can then filter by attributes like group ID, email, or name.

Next steps for clearing a Magento 2 cart with a URL

Setting up a URL to clear the cart in Magento 2 is one of those quality-of-life features that can be incredibly helpful—especially in testing, custom workflows, or even support troubleshooting.

The approach is straightforward: register a custom module, set up a new route, and use Magento’s cart model to clear out the items. Just don’t forget to secure your controller and test everything in a staging environment first.

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