If you don't know how to create module, use the link, then use my code below
Create index.php
in VendorName/ModuleName/Block/CustomerAccount
<?php
namespace VendorName\ModuleName\Block\CustomerAccount;
use Magento\Checkout\Model\Cart as CustomerCart;
class Index extends \Magento\Framework\View\Element\Template
{
protected $checkoutSession;
protected $cart;
protected $_urlInterface;
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Framework\UrlInterface $urlInterface,
\Magento\Checkout\Model\Session $checkoutSession,
CustomerCart $cart,
array $data = [])
{
$this->checkoutSession = $checkoutSession;
$this->cart = $cart;
$this->_urlInterface = $urlInterface;
parent::__construct($context, $data);
}
public function getCurrUrl()
{
return $this->_urlInterface->getCurrentUrl();
}
public function removeActiveCart(){
$allItems = $this->checkoutSession->getQuote()->getAllVisibleItems();
foreach ($allItems as $item) {
$itemId = $item->getItemId();
$this->cart->removeItem($itemId)->save();
}
}
}
Create default.xml
in VendorName/ModuleName/view/frontend/layout
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="customer_account_navigation">
<block class="VendorName\ModuleName\Block\CustomerAccount\Index" name="customer_account" template="VendorName_ModuleName::removecart.phtml" />
</referenceBlock>
</body>
</page>
Create removecart.phtml
in VendorName/ModuleName/view/frontend/templates
<?php
$currentUrl= $this->getCurrUrl();
if(isset( $_POST['remove']) AND $_POST['remove']=='true'){
$this->removeActiveCart();
}
?>
<form action="<?php echo $currentUrl; ?>" method="POST" id="removecart">
<fieldset>
<input type='hidden' name='remove' value="true">
</fieldset>
<button type="submit"><?php echo __("Remove Active Cart");?></button>
</form>