odoo/odoo · error · UserError

You cannot perform this action on an account that contains j

Error message

You cannot perform this action on an account that contains journal items.

What it means

UserError raised by the @api.ondelete(at_uninstall=False) hook _unlink_except_default() on barcodes.nomenclature when the unlink recordset includes the record referenced by the external ID barcodes.default_barcode_nomenclature. Odoo keeps this shipped nomenclature as a guaranteed default; the guard blocks its deletion during normal use but allows it at module uninstall.

Source

Thrown at addons/account/models/account_account.py:1158

                ],
                ['code_store'],
            ):
                duplicate_codes = duplicates.mapped('code')
            if duplicate_codes:
                raise ValidationError(
                    _("Account codes must be unique. You can't create accounts with these duplicate codes: %s", ", ".join(duplicate_codes))
                )

    def _load_records_write(self, values):
        if 'prefix' in values:
            del values['code_digits']
            del values['prefix']
        super()._load_records_write(values)

    @api.ondelete(at_uninstall=False)
    def _unlink_except_contains_journal_items(self):
        if self.env['account.move.line'].sudo().search_count([('account_id', 'in', self.ids)], limit=1):
            raise UserError(_('You cannot perform this action on an account that contains journal items.'))

    @api.ondelete(at_uninstall=False)
    def _unlink_except_linked_to_fiscal_position(self):
        if self.env['account.fiscal.position.account'].search_count(['|', ('account_src_id', 'in', self.ids), ('account_dest_id', 'in', self.ids)], limit=1):
            raise UserError(_('You cannot remove/deactivate the accounts "%s" which are set on the account mapping of a fiscal position.', ', '.join(f"{a.code} - {a.name}" for a in self)))

    @api.ondelete(at_uninstall=False)
    def _unlink_except_linked_to_tax_repartition_line(self):
        if self.env['account.tax.repartition.line'].search_count([('account_id', 'in', self.ids)], limit=1):
            raise UserError(_('You cannot remove/deactivate the accounts "%s" which are set on a tax repartition line.', ', '.join(f"{a.code} - {a.name}" for a in self)))

    def action_open_related_taxes(self):
        related_taxes_ids = self.env['account.tax'].search([
            ('repartition_line_ids.account_id', '=', self.id),
        ]).ids
        return {
            'type': 'ir.actions.act_window',
            'name': _('Taxes'),

View on GitHub (pinned to 1e661df964)

Solutions

  1. Exclude the default record from the delete set
  2. If you want a different default, point the barcodes.default_barcode_nomenclature XMLID reference elsewhere (a module override) — do not just unlink it
  3. Adjust cleanup scripts to filter by id != env.ref('barcodes.default_barcode_nomenclature').id

Example fix

# before
env['barcodes.nomenclature'].search([]).unlink()

# after
default = env.ref('barcodes.default_barcode_nomenclature', raise_if_not_found=False)
noms = env['barcodes.nomenclature'].search([])
(noms - default).unlink() if default else noms.unlink()
Defensive patterns

Strategy: validation

Validate before calling

default = self.env.ref('barcodes.default_barcode_nomenclature', raise_if_not_found=False)
noms = self.env['barcodes.nomenclature'].search([])
if default:
    noms -= default
noms.unlink()

Try / catch

from odoo.exceptions import UserError
try:
    noms.unlink()
except UserError as e:
    if 'default barcode nomenclature' in str(e):
        # retry without the default record
        (noms - self.env.ref('barcodes.default_barcode_nomenclature')).unlink()
    else:
        raise

Prevention

When it happens

Trigger: Calling unlink() on a recordset containing the default nomenclature — deleting it from the Barcode Nomenclature list view, or a server script purging all nomenclatures (e.g. env['barcodes.nomenclature'].search([]).unlink()).

Common situations: Data-cleanup scripts that unlink everything; admins deleting 'unused' nomenclatures; duplicated nomenclature lists where users select-all and delete.

Related errors


AI-assisted analysis of odoo/odoo@1e661df964 (2026-08-15). Data as JSON: /api/errors/fa47294b2f603378. Report an issue: GitHub.