{"id":484,"date":"2019-12-16T18:07:23","date_gmt":"2019-12-16T18:07:23","guid":{"rendered":"https:\/\/mellowhost.com\/blog\/?p=484"},"modified":"2019-12-28T05:56:32","modified_gmt":"2019-12-28T05:56:32","slug":"how-to-empty-a-model-in-odoo-mass-delete-in-odoo-mass-unlink","status":"publish","type":"post","link":"https:\/\/mellowhost.com\/blog\/how-to-empty-a-model-in-odoo-mass-delete-in-odoo-mass-unlink.html","title":{"rendered":"How to Empty a Model In Odoo \/ Mass Delete in Odoo \/ Mass Unlink"},"content":{"rendered":"\n<p>In Odoo, we use unlink() ORM method to delete a record. But if you are trying to empty a model or wants to delete multiple records, the best way to do it, is to use two steps. <\/p>\n\n\n\n<p>First, search the records:<\/p>\n\n\n\n<p><code>record_set = self.env['your.model'].search([])<\/code><\/p>\n\n\n\n<p>Second, unlink them all at once instead of looping through them:<\/p>\n\n\n\n<p><code>record_set.unlink()<\/code><\/p>\n\n\n\n<p>If you have a list of ids, search them using the list:<\/p>\n\n\n\n<p><code>ids = [1, 2, 3, 10, 11, 12]<br>record_set = self.env['your.model'].search([('id', 'in', ids)])<\/code><\/p>\n\n\n\n<p>and unlink:<\/p>\n\n\n\n<p><code>record_set.unlink()<\/code><\/p>\n\n\n\n<p>Remember, there is no need to loop through this iterable object, odoo unlink does it for you.<\/p>\n\n\n\n<p>TIPS: If you want to get all the ids in a model directly in a list, you can use the following:<br><br><code>record_list = self.env['your.model'].search([]).ids<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Odoo, we use unlink() ORM method to delete a record. But if you are trying to empty a model or wants to delete multiple records, the best way to do it, is to use two steps. First, search the records: record_set = self.env[&#8216;your.model&#8217;].search([]) Second, unlink them all at once instead of looping through them: &hellip; <a href=\"https:\/\/mellowhost.com\/blog\/how-to-empty-a-model-in-odoo-mass-delete-in-odoo-mass-unlink.html\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Empty a Model In Odoo \/ Mass Delete in Odoo \/ Mass Unlink&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[144,292,271,293],"tags":[304,297,309,308,310,311,306,301,305,307],"_links":{"self":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/484"}],"collection":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/comments?post=484"}],"version-history":[{"count":2,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/484\/revisions"}],"predecessor-version":[{"id":487,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/484\/revisions\/487"}],"wp:attachment":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/media?parent=484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/categories?post=484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/tags?post=484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}