'Takes care of deleting any outdated ir.attachment records associated to a bundle before
saving a fresh one.
When `type` is css we need to check that we are deleting a different version (and not *any*
version) because css may be paginated and, therefore, may produce multiple attachments for
the same bundle\'s version.
When `type` is js we need to check that we are deleting a different version (and not *any*
version) because, as one of the creates in `save_attachment` can trigger a rollback, the
call to `clean_attachments ` is made at the end of the method in order to avoid the rollback
of an ir.attachment unlink (because we cannot rollback a removal on the filestore), thus we
must exclude the current bundle.'

def clean_attachments(self, type):

    ira = self.env['ir.attachment']
    domain = [('url', '=like', '/web/content/%-%/{0}%.{1}'.format(self.name, type)), '!', ('url', '=like', '/web/content/%-{}/%'.format(self.version))]
    self.env['ir.qweb'].clear_caches()
    return ira.sudo().search(domain).unlink()
