moodle/moodle · warning · core_comment\comment_exception

nopermissiontodelentry

nopermissiontodelentry

Error message

nopermissiontodelentry

What it means

The comments external service delete_comments throws comment_exception 'nopermissiontodelentry' when core_comment\manager::can_delete() reports the current user is not allowed to delete the target comment. This is an authorization gate before any deletion happens, so nothing is removed when it fires. It protects comments from being deleted by users who neither own them nor hold moderation rights in the context.

Solutions

  1. Grant the calling user the required capability (moodle/comment:delete or module equivalent) in the comment's context, e.g. via role capability overrides.
  2. Verify the user actually owns the comment if ownership is the intended deletion path.
  3. Check in client code that the comment id and context belong to the acting user's scope before calling delete_comments.
  4. If the exception is expected, catch core_comment\comment_exception and surface a friendly 'not allowed' message.
  5. Re-test with test_delete_comment_other_student-style fixtures to confirm role assignment and capability settings.

Example fix

// before
delete_comments([['commentid' => $otherUserCommentId]]); // throws nopermissiontodelentry
// after
if (comment_delete_allowed($otherUserCommentId)) { // pre-check ownership/capability
    delete_comments([['commentid' => $otherUserCommentId]]);
} else {
    throw new moodle_exception('nopermissiontodelentry', 'moodle');
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Moodle: check capability before calling the external function
$context = context::instance_by_id($contextid);
if (!has_capability('moodle/comment:delete', $context) && !comment_is_owned_by_user($commentid, $USER->id)) {
    throw new moodle_exception('nopermissiontodelentry', 'moodle');
}

Try / catch

try {
    delete_comments([['commentid' => $commentid]]);
} catch (\core_comment\comment_exception $e) {
    if ($e->errorcode === 'nopermissiontodelentry') {
        // surface a friendly denial message
    } else {
        throw $e;
    }
}

Prevention

When it happens

Trigger: Calling the external function delete_comments (or the WS equivalent) with a commentid whose comment belongs to another user while the caller lacks mod/comment:delete capability in that context; also hit by tests test_delete_student_external, test_delete_admin_external, test_delete_comments_own_user, test_delete_comment_other_student and test_delete_comments_as_teacher for various permission setups.

Common situations: A student tries to delete another student's comment via the web service; a teacher role lacks the delete capability after capability overrides were changed; a client passes a comment id from a different course/module context where the user is a mere participant.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/fd0249dc4e58f2c0. Report an issue: GitHub.

Appendix: source

Thrown at public/comment/classes/external.php:346

            // Validate the context.
            list($context, $course, $cm) = get_context_info_array($commentrecord->contextid);
            if ($context->id == SYSCONTEXTID) {
                $course = $SITE;
            }
            self::validate_context($context);

            // Make sure the user is allowed to delete the comment.
            $args = new stdClass;
            $args->context   = $context;
            $args->course    = $course;
            $args->cm        = $cm;
            $args->component = $commentrecord->component;
            $args->itemid    = $commentrecord->itemid;
            $args->area      = $commentrecord->commentarea;
            $manager = new \core_comment\manager($args);

            if (!$manager->can_delete($commentrecord)) {
                throw new \core_comment\comment_exception('nopermissiontodelentry');
            }

            // User is allowed to delete it, so store the comment object, for use below in final deletion.
            $comments[$commentrecord->id] = $manager;
        }

        // All comments can be deleted by the user. Make it so.
        foreach ($comments as $commentid => $comment) {
            $comment->delete($commentid);
        }

        return [];
    }

    /**
     * Returns description of method result value for the delete_comments() method.
     *
     * @return \core_external\external_description

View on GitHub (pinned to 48ea8c3322)