-1

I'd like to delete all Sales Order/Quotations in Odoo 11.

For quotations, I was using the Odoo GUI to batch delete them. But for Sale Orders, I must firstly cancel them before deleting. This is time-consuming by the GUI way (because we cannot mass cancel Sale Orders).

So I think doing that programmatically would be better (in terms of time). However, the current code

orders = self.env['sale.order'].search([('external_id', '!=', '')])
for order in orders:
    order.unlink()

raises an exception of

You can not delete a sent quotation or a sales order! Try to cancel it before.

How can I cancel order before unlink()ing them?

Vinh VO
  • 705
  • 1
  • 7
  • 28

1 Answers1

2

Solved. After diving a bit deeper in odoo source code. I think this is what I was looking for:

orders = self.env['sale.order'].search([('external_id', '!=', '')])
for order in orders:
    order.action_cancel()
    order.unlink()
Vinh VO
  • 705
  • 1
  • 7
  • 28