'Delete histories'

def _list_delete(self, trans, histories, purge=False):

    n_deleted = 0
    deleted_current = False
    message_parts = []
    status = SUCCESS
    for history in histories:
        if history.users_shared_with:
            message_parts.append(('History    (%s)    has    been    shared    with    others,    unshare    it    before    deleting    it.        ' % history.name))
            status = ERROR
        else:
            if (not history.deleted):
                history.deleted = True
                if (history == trans.get_history()):
                    deleted_current = True
                trans.log_event(('History    (%s)    marked    as    deleted' % history.name))
                n_deleted += 1
            if (purge and trans.app.config.allow_user_dataset_purge):
                for hda in history.datasets:
                    if trans.user:
                        trans.user.adjust_total_disk_usage((- hda.quota_amount(trans.user)))
                    hda.purged = True
                    trans.sa_session.add(hda)
                    trans.log_event(('HDA    id    %s    has    been    purged' % hda.id))
                    trans.sa_session.flush()
                    if hda.dataset.user_can_purge:
                        try:
                            hda.dataset.full_delete()
                            trans.log_event(('Dataset    id    %s    has    been    purged    upon    the    the    purge    of    HDA    id    %s' % (hda.dataset.id, hda.id)))
                            trans.sa_session.add(hda.dataset)
                        except:
                            log.exception(('Unable    to    purge    dataset    (%s)    on    purge    of    hda    (%s):' % (hda.dataset.id, hda.id)))
                history.purged = True
                self.sa_session.add(history)
                self.sa_session.flush()
            for hda in history.datasets:
                if hda.creating_job_associations:
                    job = hda.creating_job_associations[0].job
                    if ((job.history_id == history.id) and (not job.finished)):
                        job.mark_deleted(trans.app.config.track_jobs_in_database)
                        trans.app.job_manager.job_stop_queue.put(job.id)
    trans.sa_session.flush()
    if n_deleted:
        part = ('Deleted    %d    %s' % (n_deleted, iff((n_deleted != 1), 'histories', 'history')))
        if (purge and trans.app.config.allow_user_dataset_purge):
            part += ('    and    removed    %s    dataset%s    from    disk' % (iff((n_deleted != 1), 'their', 'its'), iff((n_deleted != 1), 's', '')))
        elif purge:
            part += '    but    the    datasets    were    not    removed    from    disk    because    that    feature    is    not    enabled    in    this    Galaxy    instance'
        message_parts.append(('%s.        ' % part))
    if deleted_current:
        trans.get_or_create_default_history()
        message_parts.append('Your    active    history    was    deleted,    a    new    empty    history    is    now    active.        ')
        status = INFO
    return (status, '    '.join(message_parts))
