◦ Comprehensive security
◦ 24/7 support
Magento Guide → Cart → 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:
- A custom module to hold your logic
- A controller file that accesses the cart and removes items
- A route that triggers the controller when someone visits a specific URL (like yourstore.com/clearcart/index/index)
- A method like truncate() to clear all cart items at once
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.
- Navigate to your Magento root directory and create the following folder structure:
app/code/Vendor/CartClear
Replace Vendor with your actual namespace. You can name it whatever you like, but for consistency, stick with capitalized words. - Inside that folder, create the registration.php file:
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Vendor_CartClear',
__DIR__
);- Next, create the module.xml file in app/code/Vendor/CartClear/etc/
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_CartClear" setup_version="1.0.0"/>
</config>- Enable your module by running:
php bin/magento setup:upgrade
php bin/magento module:enable Vendor_CartClear
php bin/magento cache:clean
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/:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="clearcart" frontName="clearcart">
<module name="Vendor_CartClear"/>
</route>
</router>
</config>- id=”clearcart” sets the internal name of the route.
- frontName=”clearcart” defines the part of the URL that comes after your domain.
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.
- Create this file:
app/code/Vendor/CartClear/Controller/Index/Index.php - Paste the following code inside:
<?php
namespace Vendor\CartClear\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Checkout\Model\Cart;
class Index extends Action
{
protected $cart;
public function __construct(Context $context, Cart $cart)
{
$this->cart = $cart;
parent::__construct($context);
}
public function execute()
{
$this->cart->truncate()->save(); // Clears all cart items
return $this->_redirect('checkout/cart'); // Redirects to
cart page
}
}What’s happening here:
- The constructor injects the Cart model, which lets you access the user’s shopping cart.
- The truncate() method deletes all cart items at once.
- save() commits the changes.
- _redirect() sends the user to the cart page after it’s emptied.
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:
public function execute()
{
foreach ($this->cart->getQuote()->getAllItems() as $item) {
$this->cart->removeItem($item->getItemId());
}
$this->cart->save();
return $this->_redirect('checkout/cart');
}This approach is useful if you want to:
- Exclude promotional items
- Clear items from specific categories only
- Create logic around custom product attributes
Testing and security tips
While it’s convenient to have a URL that clears the cart, this opens up security considerations.
Make sure to:
- Restrict access to logged-in users: Inject \Magento\Customer\Model\Session and check if the user is logged in before running the logic.
- Add a token: Include a random string in the URL (like clearcart/index/index?token=xyz) and validate it in your controller to avoid abuse.
- Avoid using this in production without protections: You don’t want search engines or bots triggering your cart-clear URL accidentally.
- Log the action: Use Magento’s logger to record when and how often the URL is accessed. This can help you debug or prevent misuse.
Magento 2 cart clearing FAQs
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