Magento 1.9 deleting 2 products consequently from mini cart shows an error : 'Invalid form key'

Magento 1.9 deleting 2 products consequently from mini cart shows an error : 'Invalid form key'

September 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 1.9 deleting 2 products consequently from mini cart shows an error : 'Invalid form key'

Magento 1.9: Resolving the “Invalid form key” Error When Deleting Multiple Products from Mini Cart

As a Magento developer, I often encounter issues related to form keys while working with version 1.9 of this popular e-commerce platform. One such common problem is the appearance of the ‘Invalid form key’ error when attempting to delete multiple products consecutively from the mini cart without reloading the page.

The root cause for this issue lies in Magento’s implementation of form key protection against Cross-Site Request Forgery (CSRF) attacks. When you initiate an AJAX request to delete a product, the system expects a valid form key. However, if another deletion request is made immediately after the first one without refreshing the page, Magento fails to update the form key accordingly, leading to this error.

Understanding the Error Trace

The provided trace indicates that the error originates from Mage_Checkout_CartController.php. The code at line 604 (Mage::throwException('Invalid form key.');) is responsible for throwing an exception when a form key validation fails. This exception is then caught by Magento’s core controller logic and displayed as a message to the user.

The subsequent lines in the trace show how the request flows through the standard Magento routing process before finally reaching this point of failure.

Resolving the “Invalid Form Key” Issue

To resolve this issue, you need to ensure that the form key is properly refreshed after each deletion operation. This can be achieved by implementing a small JavaScript logic in your custom theme or module.

Here’s a basic example of how you could handle this:

// Call this function after successfully deleting a product via AJAX
function refreshFormKey() {
    $.ajax({
        url: '<?php echo Mage::getUrl('checkout/cart/refreshFormKey') ?>',
        type: 'POST',
        dataType: 'json',
        async: false,
        success: function(response) {
            if (response.status == 'SUCCESS') {
                $('input[name="form_key"]').val(response.formKey);
            }
        }
    });
}

In this snippet, we’re making an AJAX call to a custom controller action (refreshFormKey) that returns the current valid form key. We then update the form key input field with the new value.

Creating the Custom Controller Action

To create the refreshFormKey action, you can extend the Mage_Checkout_CartController.

class Mage_Checkout_CartController extends Mage_Checkout_CartController
{
    public function refreshFormKeyAction()
    {
        $formKey = Mage::getSingleton('core/session')->getFormKey();
        echo '{"status":"SUCCESS","formKey":"' . $formKey . '"}';
    }
}

Ensure you register this action in your module’s config.xml file:

<config>
    <modules>
        <Namespace_Checkout>
            <version>1.0.0</version>
        </Namespace_Checkout>
    </modules>
    <global>
        <controllers>
            <checkout>
                <rewrite>
                    <cart>Namespace_Checkout_CartController</cart>
                </rewrite>
            </checkout>
        </controllers>
    </global>
</config>

Conclusion

Resolving the ‘Invalid form key’ error in Magento 1.9 when deleting multiple products from the mini cart involves a careful understanding of the platform’s security mechanisms and proper handling of form keys across AJAX requests.

By implementing the JavaScript logic to refresh the form key after each deletion, you can ensure the system maintains consistent state management and prevents this common issue from occurring.

Remember, working with Magento’s core controllers and sessions requires a deep understanding of the platform’s architecture. Always refer to the official documentation for detailed guides on extending and customizing the platform: Magento 1.x Developer Documentation

Magento 1 Checkout Catalog Errors

Our network