XML stands for Extensible Markup Language which is a markup language that defines a set of rules for encoding documents. The XML document is both human-readable and machine-readable. It is much more like HTML. XML is used to store and transport data and is also designed to be self-descriptive.
It is using the XML codes, the Odoo views are created. Data files in Odoo are also defined using XML.
As we know, we can delete records from the UI ( using the delete button in the action in the record) or from the python function. Suppose you need to delete records from the database or install some custom modules, so in that case, you need to write some extra fields, and for the proper working of your module you may need to delete some records from the database. So that may be the case when you need to delete the records.
This blog explains how to delete a record in the database from the XML code itself. We know how to create and update records from XML files. But in case of dependent modules sometimes we need to delete the previously created records from the XML file. In that case, we can use the tag <delete>.
So let’s see how it works.
There are two ways to delete records from XML as follows:
1. Using the XML ID of the previously created records.
2. Using the search Domain.
In both the methods you have to specify the model. That is in order to delete a record from the model you need to specify the model name in the attribute ‘model’ in the tag <delete>.
1. Using the XML ID
In this method of deleting records from XML you have to follow the syntax as given below:
<delete model = “your model name” id = “XML ID of the record”/>
Here you have to specify the id, which is the XML ID of the records that were previously created from another module’s data files. And also you have to specify the attribute model, that is the model in which the record lies. The attribute model is a compulsory attribute for deleting the record. So while running the above code (or during the installation of your custom module ) odoo will find the record using the XML ID that you have given. So if there is any record corresponding to the given XML ID it will delete that record . And suppose it doesn’t find any matching record it will raise an error. You can only delete the records that are created from the XML files (or records that have the XML IDs).
2. Using the search domain
In this method, you need to pass the domain in the domain attribute. So during the installation of your custom modules odoo will try to find the records based on the domain that you have given. If records are found, odoo will delete those matching records. Compared to the previous method this method will not raise an error if no records are found matching the given domain. So while using this method you have to be more careful as it will delete your user’s data since the search option deletes all the records that match the domain.
The syntax is as follows:
suppose if you want to delete records from sale.order if the customer is ‘Azure Interior’:
<delete model = “sale.order” search [(partner_id.name,’=’,’Azure Interior’)]/>
In odoo delete is used very rarely as it becomes dangerous in many situations.