'Odoo : How to clear a many2many field when a many2one field change?

Im new to odoo. Im using odoo 13 and I have a many2one field and a many2many field with domain on the many2one field. What I need is that if the user changes the many2one field the many2many gets empty and ready to choose. but I don't know how to code that on a function so I have tried some variants but it just dont come to me. If I use this I just get an error when Im going to select for the first time the 'industria' field

class Partner(models.Model):
    _inherit = 'res.partner'
    industria = fields.Many2one('industria_model', string="Industria")
    marca = fields.Many2many('marca_model',
                             domain="[('industria_id', '=', industria)]", string="Marca")

    @api.onchange('industria')
    def get_price(self):
        self.marca = [(5)]
        # if self.marca:
        #     self.marca = [(5, 0, 0)]


Solution 1:[1]

[(5)] will be evaluated to [5] then Odoo will try to return the corresponding record that is not created yet (at the first time) and you should see the following error:

Record does not exist or has been deleted. (Record: marca_model(5,), 

You can simply use [(5,)] to empty the marca field.

If a record with id 5 exists in database, you will get the same result as using [(4, 5)]

Solution 2:[2]

In the end I used this

@api.onchange('industria')
    def change_brand(self):
        self.marca = [(6, 0, [])]

I had already tested it but I had to restart odoo to see the changes, just updating the module didn't worked

Solution 3:[3]

you can delete your many2many's records with 3 way like this:

@api.onchange('industria')
def change_brand(self):
    for rec in self.marca:
        self.marca = [(3, rec.id, 0)] # only delete this record from m2m field
        # self.marca = [2,rec.id,0] 
        # self.marca = [5,0,0] 

[(3, rec.id, 0)] means this record delete only from the form view but it is in related table until user click on save or create button, and [2,rec.id,0] means this record delete from both table and from view, and [5,0,0] deletes all of many2many records from both table and form view. I used all of them and they worked. Hope it has been useful :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kenly
Solution 2 Armando Peña
Solution 3 reyhane janboori