'Mark deleted all Datasets whose associations are all marked as deleted (LDDA) or purged (HDA) that are older than the specified number of days.'

def delete_datasets(self):

    log.info('Marking    deleted    all    Datasets    whose    associations    are    all    marked    as    deleted/purged    that    are    older    than    the    specified    number    of    days.')
    event_id = self._create_event()
    sql = "\n                                                                WITH    deleted_dataset_ids\n                                                                        AS    (                    UPDATE    dataset\n                                                                                                                        SET    deleted    =    true%s\n                                                                                                                WHERE    NOT    deleted\n                                                                                                                                        AND    NOT    EXISTS    (SELECT    true\n                                                                                                                                                                                                                FROM    library_dataset_dataset_association\n                                                                                                                                                                                                            WHERE    (NOT    deleted\n                                                                                                                                                                                                                                        OR    update_time    >=    (NOW()    -    interval    '%s    days'))\n                                                                                                                                                                                                                                    AND    dataset.id    =    dataset_id)\n                                                                                                                                        AND    NOT    EXISTS    (SELECT    true\n                                                                                                                                                                                                                FROM    history_dataset_association\n                                                                                                                                                                                                            WHERE    (NOT    purged\n                                                                                                                                                                                                                                        OR    update_time    >=    (NOW()    -    interval    '%s    days'))\n                                                                                                                                                                                                                                    AND    dataset.id    =    dataset_id)\n                                                                                                RETURNING    id),\n                                                                                    dataset_events\n                                                                        AS    (INSERT    INTO    cleanup_event_dataset_association\n                                                                                                                                        (create_time,    cleanup_event_id,    dataset_id)\n                                                                                                            SELECT    NOW(),    %s,    id\n                                                                                                                    FROM    deleted_dataset_ids)\n                                                        SELECT    id\n                                                                FROM    deleted_dataset_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', '%s'))
    args = (self.options.days, self.options.days, event_id)
    cur = self._update(sql, args)
    self._flush()
    self._open_logfile()
    for tup in cur:
        self._log(('Marked    Dataset    deleted:    %s' % tup[0]))
    self._close_logfile()
