'Mark purged all HistoryDatasetAssociations currently marked deleted that are older than the specified number of days.
Mark deleted all MetadataFiles whose hda_id is purged in this step.
Mark deleted all ImplicitlyConvertedDatasetAssociations whose hda_parent_id is purged in this step.
Mark purged all HistoryDatasetAssociations for which an ImplicitlyConvertedDatasetAssociation with matching hda_id is deleted in this step.'

def purge_deleted_hdas(self):

    log.info(('Marking    purged    all    deleted    HistoryDatasetAssociations    older    than    %i    days' % self.options.days))
    event_id = self._create_event()
    sql = "\n                                                        WITH    purged_hda_ids\n                                                                AS    (                    UPDATE    history_dataset_association\n                                                                                                                SET    purged    =    true%s\n                                                                                                        WHERE    deleted%s\n                                                                                                                                AND    update_time    <    (NOW()    -    interval    '%s    days')\n                                                                                        RETURNING    id,\n                                                                                                                                history_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                                                                            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_hda_ids.id,\n                                                                            history.user_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_hda_ids\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                                                                            LEFT    OUTER    JOIN    history\n                                                                                                                                            ON    purged_hda_ids.history_id    =    history.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, '%s', '%s', '%s', '%s'))
    args = (self.options.days, 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    HistoryDatasetAssociations    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    HDA    %s    caused    deletion    of    MetadataFile:    %s    in    Object    Store:    %s' % (tup[0], tup[2], tup[3])))
            self._remove_metadata_file(tup[2], tup[3], inspect.stack()[0][3])
        if (tup[4] is not None):
            self._log(('Purge    of    HDA    %s    caused    deletion    of    ImplicitlyConvertedDatasetAssociation:    %s    and    converted    HistoryDatasetAssociation:    %s' % (tup[0], tup[4], tup[5])))
    self._close_logfile()
