Deleted orders are not removed from grid
-
12-12-2019 - |
Question
When I delete an order, it is still listed in orders grid. When I attempt to open it, I get the message
This order no longer exists.
This behaviour was seemingly introduced by a Magento update: I downloaded the latest version of Magento and overrode the existing files.
Why do the orders still show up in the grid and how can I adjust this?
Solution
As others have said, you have orphaned records in your sales_flat_order_grid
table that are not in your main order table, sales_flat_order
. I'm noting two non-native behaviors on your instance of Magento.
You can delete orders. Default Magento does not let you delete order. I don't recommend allowing orders to be deleted in live environment.
Your MySQL constraint (
CONSTRAINT... ON DELETE CASCADE
) on the grid table has been removed. When an order record is deleted (via any method), The DB is supposed to automatically remove all associated records insales_flat_order_*
tables, which include the grid.
You need to do two things to restore integrity to your instance.
Re-establish the MySQL constraint. This will prevent orphan order records. This seems more off-topic, I'll offer a solution to your current problem below.
Remove the orphan order grid records. Run the following query to remove them.
Back up DB before running. Test it on a development environment first. I'm not responsible for your data loss.
DELETE
FROM `sales_flat_order_grid`
WHERE entity_id IN (
SELECT * FROM (
SELECT g.entity_id
FROM `sales_flat_order_grid` AS g
INNER JOIN `sales_flat_order` AS o
ON g.`entity_id` = o.`entity_id`
WHERE g.entity_id IS NULL
) AS t
)
OTHER TIPS
Mour,
The order has been already deleted from system in table sales_flat_order
.You need to check here.
I guess that order is in sales_flat_order_grid
database table but it doesn't exits on sale_flat_order
table which is main table of sales order
model[Mage::getModel('sales/order')
]
see at class Mage_Adminhtml_Sales_OrderController
where magento is check order is exit or not.
on function initorder
protected function _initOrder()
{
$id = $this->getRequest()->getParam('order_id');
$order = Mage::getModel('sales/order')->load($id);
if (!$order->getId()) {
$this->_getSession()->addError($this->__('This order no longer exists.'));
$this->_redirect('*/*/');
$this->setFlag('', self::FLAG_NO_DISPATCH, true);
return false;
}
Mage::register('sales_order', $order);
Mage::register('current_order', $order);
return $order;
}
That means order does not delete properly.
You need to delete those order from sales_flat_order_grid.
The problem is
The order entry exists in the sales_flat_order_grid
table but missing in sales_flat_order
table
The solution is
Delete the entries from sales_flat_order_grid table which has no reference in sales_flat_order table
Run this SQL Query in your database:
DELETE FROM `sales_flat_order_grid`
WHERE entity_id NOT IN (
SELECT `entity_id` FROM `sales_flat_order`
);
Add a trigger on sales_flat_order table
on event AFTER DELETE ON sales_flat_order
if not exists,
DELETE FROM sales_flat_order_grid WHERE OLD.entity_id=sales_flat_order_grid.entity_id;