Magento: Clear / Delete Shopping Cart Items of Single or All Customers

Here is the code to delete all shopping cart items of currently logged in single customer:


$cart = Mage::getSingleton('checkout/cart'); 
$quoteItems = Mage::getSingleton('checkout/session')
                  ->getQuote()
                  ->getItemsCollection();

foreach( $quoteItems as $item ){
    $cart->removeItem( $item->getId() );    
}
$cart->save();

If you want to clear all shopping cart items of all customers then you can use the following code:


$quoteCollection = Mage::getModel('sales/quote')
			->getCollection()
			->addFieldToFilter('is_active', 1);
						
foreach ($quoteCollection as $item) {
	$item->delete();	
}

If you have a large number of customers quotes then deleting them by using loop might be time and resource consuming. You can clear/delete all customers cart items (all active sales quotes) by using the following SQL query:


DELETE FROM sales_flat_quote WHERE is_active = 1;

is_active = 0 means those quotes have been converted into orders, i.e. customer has placed order for those quotes.
is_active = 1 means quotes that have not been ordered, i.e. quotes present in the shopping cart of customers

Running this query will automatically delete related rows (quote items) from sales_flat_quote_item table through foreign key constraint.

Hope it helps. Thanks.