Delete data from XML
Using XML, we can create or update records in Odoo. In some cases, we need to delete the
previously created data from the database while installing some custom modules for the
proper working of that module. In that case, we can use the tag <delete> .
There are two ways to delete records from XML, as given below:-
- 1. Using the XML ID
- 2. Using Search domain
In both ways, we need to specify the model name as the model attribute of <delete>
Using the XML ID
By using the XML ID of previously created records, we are able to delete them. The syntax
is as follows:
<delete model = "your model name" id = "XML ID of the record"/>
In this method, the id attribute refers to the XML ID of the previously created record
from another module data file. The model attribute refers to the model name in which the
record exists. Using <delete> we are only able to delete the records created using XML
or a record having an external ID. Suppose we have created a data using an XML file
in product.category as follows:
<record id="demo_category_1" model="product.category">
<field name="name">Category 1</field>
</record>
An example of deleting the record created from another module using XML ID as given
below:
<delete model = "product.category" id = "demo_category_1"/>
While running the above code odoo will find the record with XML ID ‘demo_category_1’ in
model ‘product.category’, if found then it will be deleted. If it is not found, then
raise an error.
Using the search domain
In this method, we need to pass the domain in the attribute. Then, while installing our
module, odoo tries to find out the records from the given model with the domain we are
passed. If records are found, then the data gets removed. Compared to the first method
this method didn’t raise an error in case of matching records with the domain not found.
While using this method for , we need to be more careful. Because it will delete
the user’s data when it matches the domain, that is the search option deletes all
the records that satisfy the domain.
The syntax is as follows:
<delete model = "model_name" search="domain"/>
An example for deleting the record using the search domain is given below:
<delete model = “sale.order” search [(partner_id.name,’=’,’Azure Interior’)]/>
While running the above code odoo will delete the sale order with the customer name as
‘Azure Interior’.
In odoo, delete is rarely used because it becomes dangerous in some scenarios.