final public function delete($downloadcontents = false) {
        global $DB;
        if ($downloadcontents) {
            $this->convert_references_to_local();
        } else {
            $this->remove_files();
        }
        cache::make('core', 'repositories')->purge();
        try {
            $DB->delete_records('repository_instances', array('id'=>$this->id));
            $DB->delete_records('repository_instance_config', array('instanceid'=>$this->id));
        } catch (dml_exception $ex) {
            return false;
        }
        return true;
    }