Cleans up locks that have not been removed correctly code locks = env['muk_security.lock'] for lock in model.search([]): if not lock.lock_ref: locks |= lock locks.unlink()