public function delete() {
        global $DB;

        if ($this->_contextlevel <= CONTEXT_SYSTEM) {
            throw new coding_exception('Cannot delete system context');
        }

        // double check the context still exists
        if (!$DB->record_exists('context', array('id'=>$this->_id))) {
            context::cache_remove($this);
            return;
        }

        $this->delete_content();
        $DB->delete_records('context', array('id'=>$this->_id));
        // purge static context cache if entry present
        context::cache_remove($this);
    }