Recently Viewed Products in Magento

Recently Viewed Products in Magento

October 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Recently Viewed Products in Magento

Clearing Recently Viewed Products in Magento

As an e-commerce developer, you might find yourself needing to clear recently viewed products in Magento for your customers. This is particularly useful when testing or debugging issues related to this feature.

Firstly, let’s understand what the “Recently Viewed Products” block does:

By default, Magento keeps track of all the products a logged-in customer views and displays them on the right sidebar under the heading “Recently Viewed”. This helps customers navigate back to items they’ve shown interest in without having to search for them again.

The Challenge: Clearing Recently Viewed Products

Now, let’s tackle your specific request—clearing all recently viewed products when a customer clicks a “Clear Recently Viewed Products” link. To achieve this, we’ll need to manipulate the Magento session and clear the product IDs stored there.

Before proceeding, make sure you have a good understanding of how Magento handles sessions and data persistence across requests.

Clearing Recently Viewed Products Programmatically

To remove all recently viewed products programmatically (e.g., when a customer clicks a link), you’ll need to access and manipulate the session data directly. Here’s an example function that should do this:

public function clearRecentlyViewedProducts()
{
    $session = Mage::getSingleton('core/session', array('name' => 'customer'));
    $recently_viewed_products = (array)$session->getRecentlyViewedProducts();

    if (!empty($recently_viewed_products)) {
        foreach ($recently_viewed_products as $product_id) {
            unset($recently_viewed_products[$product_id]);
        }
    }

    // Save the updated array back to the session
    $session->setRecentlyViewedProducts($recently_viewed_products);
}

In this code:

  1. We retrieve the customer’s session.
  2. Get the recently viewed products from that session.
  3. Loop through each product ID and unset it from the array using unset().
  4. Save the updated (now empty) array back to the session.

Ensure you place this function in a controller or block file accessible during the frontend rendering process, ideally within your theme’s template files where the “Recently Viewed Products” block is rendered.

Displaying an Empty Message

After clearing the recently viewed products using the above function, Magento should automatically display the message “There are no recently viewed product available”. This behavior is built-in to the core Magento functionality.

However, if you want to customize this message or add some JavaScript effects when clicking the clear link, you might need to extend the default “Recently Viewed Products” block and override its template file. For detailed instructions on this process, I recommend checking out our guide on Overriding Magento Blocks.

Conclusion

Clearing recently viewed products in Magento is a straightforward task once you understand how the session data works and where to manipulate it. By using the provided code snippet, you can easily create a “Clear Recently Viewed Products” link that effectively removes all product IDs from the customer’s session.

Remember, always test changes like these thoroughly on staging servers before deploying them to live environments to ensure they don’t introduce any unexpected issues or break existing functionality.

For more advanced customizations and in-depth Magento development topics, be sure to explore our Magento Development category for comprehensive guides and best practices.

Catalog API Customers Errors

Our network