'Mark purged all Datasets marked deleted that are older than the specified number of days.'

def purge_datasets(self):

    log.info('Marking    purged    all    Datasets    marked    deleted    that    are    older    than    the    specified    number    of    days.')
    event_id = self._create_event()
    sql = "\n                                                                WITH    purged_dataset_ids\n                                                                        AS    (                    UPDATE    dataset\n                                                                                                                        SET    purged    =    true%s\n                                                                                                                WHERE    deleted%s\n                                                                                                                                        AND    update_time    <    (NOW()    -    interval    '%s    days')\n                                                                                                RETURNING    id,\n                                                                                                                                        object_store_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    purged_dataset_ids)\n                                                        SELECT    id,\n                                                                                    object_store_id\n                                                                FROM    purged_dataset_ids\n                                                ORDER    BY    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', '%s'))
    args = (self.options.days, event_id)
    cur = self._update(sql, args)
    self._flush()
    self._open_logfile()
    for tup in cur:
        self._log(('Marked    Dataset    purged:    %s    in    Object    Store:    %s' % (tup[0], tup[1])))
        dataset = Dataset(id=tup[0], object_store_id=tup[1])
        try:
            filename = self.object_store.get_filename(dataset)
        except (ObjectNotFound, AttributeError) as e:
            log.error(('Unable    to    get    Dataset    %s    filename:    %s' % (tup[0], e)))
            continue
        try:
            extra_files_dir = self.object_store.get_filename(dataset, dir_only=True, extra_dir=('dataset_%d_files' % tup[0]))
        except (ObjectNotFound, AttributeError):
            extra_files_dir = None
        self._log(('Removing    from    disk:    %s' % filename))
        if (not self.options.dry_run):
            try:
                os.unlink(filename)
            except Exception as e:
                self._log(('Removal    of    %s    failed    with    error:    %s' % (filename, e)))
        if ((extra_files_dir is not None) and os.path.exists(extra_files_dir)):
            self._log(('Removing    from    disk:    %s' % extra_files_dir))
            if (not self.options.dry_run):
                try:
                    shutil.rmtree(extra_files_dir)
                except Exception as e:
                    self._log(('Removal    of    %s    failed    with    error:    %s' % (extra_files_dir, e)))
    self._close_logfile()
