'Mark purged all Histories marked deleted that are older than the specified number of days.
Mark purged all HistoryDatasetAssociations in Histories marked purged in this step (if not already purged).'

def purge_deleted_histories(self):

    log.info('Marking    purged    all    deleted    histories    that    are    older    than    the    specified    number    of    days.')
    event_id = self._create_event()
    sql = "\n                                                        WITH    purged_history_ids\n                                                                AS    (                    UPDATE    history\n                                                                                                                SET    purged    =    true%s\n                                                                                                        WHERE    deleted%s\n                                                                                                                                AND    update_time    <    (NOW()    -    interval    '%s    days')\n                                                                                        RETURNING    id,\n                                                                                                                                user_id),\n                                                                            purged_hda_ids\n                                                                AS    (                    UPDATE    history_dataset_association\n                                                                                                                SET    purged    =    true%s\n                                                                                                            FROM    purged_history_ids\n                                                                                                        WHERE    purged_history_ids.id    =    history_dataset_association.history_id\n                                                                                                                                AND    NOT    history_dataset_association.purged\n                                                                                        RETURNING    history_dataset_association.history_id    AS    history_id,\n                                                                                                                                history_dataset_association.id    AS    id),\n                                                                            deleted_metadata_file_ids\n                                                                AS    (                    UPDATE    metadata_file\n                                                                                                                SET    deleted    =    true%s\n                                                                                                            FROM    purged_hda_ids\n                                                                                                        WHERE    purged_hda_ids.id    =    metadata_file.hda_id\n                                                                                        RETURNING    metadata_file.hda_id    AS    hda_id,\n                                                                                                                                metadata_file.id    AS    id,\n                                                                                                                                metadata_file.object_store_id    AS    object_store_id),\n                                                                            deleted_icda_ids\n                                                                AS    (                    UPDATE    implicitly_converted_dataset_association\n                                                                                                                SET    deleted    =    true%s\n                                                                                                            FROM    purged_hda_ids\n                                                                                                        WHERE    purged_hda_ids.id    =    implicitly_converted_dataset_association.hda_parent_id\n                                                                                        RETURNING    implicitly_converted_dataset_association.hda_id    AS    hda_id,\n                                                                                                                                implicitly_converted_dataset_association.hda_parent_id    AS    hda_parent_id,\n                                                                                                                                implicitly_converted_dataset_association.id    AS    id),\n                                                                            deleted_icda_purged_child_hda_ids\n                                                                AS    (                    UPDATE    history_dataset_association\n                                                                                                                SET    purged    =    true%s\n                                                                                                            FROM    deleted_icda_ids\n                                                                                                        WHERE    deleted_icda_ids.hda_id    =    history_dataset_association.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    purged_history_ids),\n                                                                            hda_events\n                                                                AS    (INSERT    INTO    cleanup_event_hda_association\n                                                                                                                                (create_time,    cleanup_event_id,    hda_id)\n                                                                                                    SELECT    NOW(),    %s,    id\n                                                                                                            FROM    purged_hda_ids),\n                                                                            metadata_file_events\n                                                                AS    (INSERT    INTO    cleanup_event_metadata_file_association\n                                                                                                                                (create_time,    cleanup_event_id,    metadata_file_id)\n                                                                                                    SELECT    NOW(),    %s,    id\n                                                                                                            FROM    deleted_metadata_file_ids),\n                                                                            icda_events\n                                                                AS    (INSERT    INTO    cleanup_event_icda_association\n                                                                                                                                (create_time,    cleanup_event_id,    icda_id)\n                                                                                                    SELECT    NOW(),    %s,    id\n                                                                                                            FROM    deleted_icda_ids),\n                                                                            icda_hda_events\n                                                                AS    (INSERT    INTO    cleanup_event_hda_association\n                                                                                                                                (create_time,    cleanup_event_id,    hda_id)\n                                                                                                    SELECT    NOW(),    %s,    hda_id\n                                                                                                            FROM    deleted_icda_ids)\n                                                SELECT    purged_history_ids.id,\n                                                                            purged_history_ids.user_id,\n                                                                            purged_hda_ids.id,\n                                                                            deleted_metadata_file_ids.id,\n                                                                            deleted_metadata_file_ids.object_store_id,\n                                                                            deleted_icda_ids.id,\n                                                                            deleted_icda_ids.hda_id\n                                                        FROM    purged_history_ids\n                                                                            LEFT    OUTER    JOIN    purged_hda_ids\n                                                                                                                                            ON    purged_history_ids.id    =    purged_hda_ids.history_id\n                                                                            LEFT    OUTER    JOIN    deleted_metadata_file_ids\n                                                                                                                                            ON    deleted_metadata_file_ids.hda_id    =    purged_hda_ids.id\n                                                                            LEFT    OUTER    JOIN    deleted_icda_ids\n                                                                                                                                            ON    deleted_icda_ids.hda_parent_id    =    purged_hda_ids.id;\n                                "
    force_retry_sql = '\n                                                                                                                                AND    NOT    purged'
    update_time_sql = ''
    if self.options.force_retry:
        force_retry_sql = ''
    elif self.options.update_time:
        update_time_sql += ',\n                                                                                                                                update_time    =    NOW()'
    sql = (sql % (update_time_sql, force_retry_sql, '%s', update_time_sql, update_time_sql, update_time_sql, update_time_sql, '%s', '%s', '%s', '%s', '%s'))
    args = (self.options.days, event_id, event_id, event_id, event_id, event_id)
    cur = self._update(sql, args)
    self._flush()
    self._open_logfile()
    for tup in cur:
        self._log(('Marked    History    purged:    %s' % tup[0]))
        if ((tup[1] is not None) and (tup[1] not in self.disk_accounting_user_ids)):
            self.disk_accounting_user_ids.append(int(tup[1]))
        if (tup[2] is not None):
            self._log(('Purge    of    History    %s    caused    deletion    of    HistoryDatasetAssociation:    %s' % (tup[0], tup[2])))
        if (tup[3] is not None):
            self._log(('Purge    of    HDA    %s    caused    deletion    of    MetadataFile:    %s    in    Object    Store:    %s' % (tup[1], tup[3], tup[4])))
            self._remove_metadata_file(tup[3], tup[4], inspect.stack()[0][3])
        if (tup[5] is not None):
            self._log(('Purge    of    HDA    %s    caused    deletion    of    ImplicitlyConvertedDatasetAssociation:    %s    and    converted    HistoryDatasetAssociation:    %s' % (tup[1], tup[5], tup[6])))
    self._close_logfile()
