'Mark deleted all "anonymous" Histories (not owned by a registered user) that are older than the specified number of days.'

def delete_userless_histories(self):

    log.info(('Marking    deleted    all    userless    Histories    older    than    %i    days' % self.options.days))
    event_id = self._create_event()
    sql = "\n                                                                WITH    deleted_history_ids\n                                                                        AS    (                    UPDATE    history\n                                                                                                                        SET    deleted    =    true%s\n                                                                                                                WHERE    user_id    is    null\n                                                                                                                                        AND    NOT    deleted\n                                                                                                                                        AND    update_time    <    (NOW()    -    interval    '%s    days')\n                                                                                                RETURNING    id),\n                                                                                    history_events\n                                                                        AS    (INSERT    INTO    cleanup_event_history_association\n                                                                                                                                        (create_time,    cleanup_event_id,    history_id)\n                                                                                                            SELECT    NOW(),    %s,    id\n                                                                                                                    FROM    deleted_history_ids)\n                                                        SELECT    id\n                                                                FROM    deleted_history_ids\n                                                ORDER    BY    id;\n                                "
    update_time_sql = ''
    if self.options.update_time:
        update_time_sql = ',\n                                                                                                                                        update_time    =    NOW()'
    sql = (sql % (update_time_sql, '%s', '%s'))
    args = (self.options.days, event_id)
    cur = self._update(sql, args)
    self._flush()
    self._open_logfile()
    for tup in cur:
        self._log(('Marked    userless    History    deleted:    %s' % tup[0]))
    self._close_logfile()
