Snippet: Bulk Reset LearnDash Quiz Attempts for a Course via WP-CLI
A WP-CLI command that removes every attempt of one quiz, or every quiz in a course, for all or selected learners, using learndash_remove_user_quiz_attempt() so statistics, leaderboards and essays are cleaned up too.

Resetting one learner's quiz attempt is a click on their profile. Resetting a quiz for three hundred learners, because a question was wrong or a course has been rebuilt for a new cohort, is not something the admin offers. This snippet adds a WP-CLI command for LearnDash 4.18.0.1 that removes attempts in bulk, by quiz or by course, with a dry run and user filters. It leans on learndash_remove_user_quiz_attempt(), which is the same function the profile button uses, so every store that holds a piece of an attempt is cleaned up consistently.
What a quiz attempt consists of
An attempt is not one row. LearnDash writes an entry to the user's _sfwd-quizzes user meta array, holding keys such as quiz, pro_quizid, course, lesson, topic, score, count, pass, percentage, points, total_points, timespent, time, started, completed, has_graded and statistic_ref_id. It also inserts a quiz row in the user activity table, optionally a set of statistics rows referenced by statistic_ref_id, a leaderboard row when the quiz has one, and for essay questions a post of type sfwd-essays referenced from the graded key. The quiz storage guide walks through each store.
learndash_remove_user_quiz_attempt( $user_id, $args ) takes care of all of them. It loads the user's attempt array, keeps every entry whose values match all key and value pairs in $args, and for each match fires learndash_user_quiz_delete_single_before, removes the leaderboard row, removes the statistics rows by reference, deletes graded essay posts, deletes the matching activity record, fires learndash_user_quiz_delete_single_after, and finally writes the reduced array back and returns the removed entries. Because matching is by exact value, passing array( 'quiz' => 123 ) removes every attempt of quiz 123 for that user, and adding 'course' => 45 narrows it to attempts taken inside course 45.
The command
<?php
/**
* Bulk reset LearnDash quiz attempts.
*
* Usage:
* wp wwc reset-quiz-attempts --quiz=123 --dry-run
* wp wwc reset-quiz-attempts --course=45 --yes
* wp wwc reset-quiz-attempts --course=45 --users=10,11,12 --yes
* wp wwc reset-quiz-attempts --quiz=123 --failed-only --yes
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'wwc reset-quiz-attempts', 'wwc_cli_reset_quiz_attempts' );
}
function wwc_cli_reset_quiz_attempts( $args, $assoc_args ) {
$quiz_id = absint( $assoc_args['quiz'] ?? 0 );
$course_id = absint( $assoc_args['course'] ?? 0 );
$dry_run = isset( $assoc_args['dry-run'] );
$failed = isset( $assoc_args['failed-only'] );
if ( ! $quiz_id && ! $course_id ) {
WP_CLI::error( 'Pass --quiz=ID or --course=ID.' );
}
// Which quizzes are in scope.
$quiz_ids = $quiz_id ? array( $quiz_id ) : learndash_course_get_steps_by_type( $course_id, learndash_get_post_type_slug( 'quiz' ) );
$quiz_ids = array_map( 'absint', (array) $quiz_ids );
if ( empty( $quiz_ids ) ) {
WP_CLI::error( 'No quizzes found for that scope.' );
}
// Which users are in scope.
if ( ! empty( $assoc_args['users'] ) ) {
$user_ids = array_map( 'absint', explode( ',', $assoc_args['users'] ) );
} else {
$user_ids = get_users(
array(
'fields' => 'ID',
'meta_key' => '_sfwd-quizzes',
'number' => -1,
)
);
}
if ( ! $dry_run ) {
WP_CLI::confirm( sprintf( 'Remove attempts of %d quiz(es) for up to %d users?', count( $quiz_ids ), count( $user_ids ) ), $assoc_args );
}
$removed = 0;
$touched = 0;
$progress = WP_CLI\Utils\make_progress_bar( 'Users', count( $user_ids ) );
foreach ( $user_ids as $user_id ) {
$attempts = get_user_meta( $user_id, '_sfwd-quizzes', true );
$progress->tick();
if ( empty( $attempts ) || ! is_array( $attempts ) ) {
continue;
}
foreach ( $quiz_ids as $qid ) {
foreach ( $attempts as $attempt ) {
if ( absint( $attempt['quiz'] ?? 0 ) !== $qid ) {
continue;
}
if ( $course_id && absint( $attempt['course'] ?? 0 ) !== $course_id ) {
continue;
}
if ( $failed && ! empty( $attempt['pass'] ) ) {
continue;
}
$removed++;
if ( $dry_run ) {
continue;
}
// Match on the attempt timestamp so only this entry is removed.
learndash_remove_user_quiz_attempt(
$user_id,
array(
'quiz' => $qid,
'time' => $attempt['time'],
)
);
}
}
$touched++;
}
$progress->finish();
if ( $dry_run ) {
WP_CLI::log( sprintf( 'Dry run: %d attempt(s) across %d user(s) would be removed.', $removed, $touched ) );
return;
}
WP_CLI::success( sprintf( 'Removed %d attempt(s) across %d user(s).', $removed, $touched ) );
}
Why match on time rather than quiz alone
Passing only 'quiz' => $qid to the removal function would delete every attempt of that quiz for the user in one call, which is simpler but makes the failed-only filter impossible, because the function matches on the arguments you give it, not on a callback. Matching on quiz plus time, the Unix timestamp of the attempt, isolates one entry while still letting LearnDash do the related cleanup. Two attempts in the same second are rare enough to ignore for a reset.
Restoring attempts left and course progress
Removing attempts restores a learner's retake allowance automatically, because LearnDash counts attempts from the same meta array when it enforces the repeat limit; the behaviour is explained in limiting quiz attempts. What a reset does not do is reopen the course step. If the quiz was the final step and the course is marked complete, the course stays complete; a passed quiz also stays ticked in the course progress tree. To push learners back to the quiz, clear that step from their course progress as well, which is covered in resetting course progress, or run the bulk reset before the course is opened to the new cohort.
Running it safely
- Take a database backup, or at least export the
_sfwd-quizzesrows for the affected users withwp db query, because removed statistics rows cannot be rebuilt. - Run with
--dry-runfirst and sanity-check the count against the quiz's Statistics screen. - Run with
--usersfor one or two test accounts and verify their profiles. - Run the full command with
--yesduring a quiet period. Each removal updates one user meta row and a handful of table rows, so a few thousand attempts take under a minute. - Clear any object cache afterwards, since the attempt array is read through the user meta cache.
Put the command in a small mu-plugin so it exists on every environment, as described in our guide to WP-CLI with LearnDash. The quiz attempt removal guide covers the related quiz lock table if your quizzes use time-based retake locks, which this command does not clear.
Scoping by group
Cohort resets are usually "everyone in this group", not every user on the site. Add a --group flag that resolves the group's members with learndash_get_groups_user_ids( $group_id ), which returns an array of user IDs, and use it in place of the meta query:
<?php
if ( ! empty( $assoc_args['group'] ) ) {
$user_ids = learndash_get_groups_user_ids( absint( $assoc_args['group'] ) );
}
Group leaders cannot run WP-CLI, so if leaders need to reset their own groups, wrap the same logic in an admin action behind a capability check and a nonce, and reuse the dry-run output as a confirmation table. Keep the command as the canonical implementation and have the admin screen call the same function, so the two never drift apart.
What the removal hooks are for
The two actions fired by the removal function, learndash_user_quiz_delete_single_before and learndash_user_quiz_delete_single_after, each receive the user ID and the attempt array. They are the right place to archive an attempt to a reporting table before it disappears, or to notify an integration that a score it synced is no longer valid. Because the command uses the same function as the profile button, a listener you add serves both paths.
Testing
On a staging copy, take a quiz three times with one account, failing twice and passing once, and once with a second account. A dry run with --quiz should report four attempts. The failed-only flag should report two and, after a live run, leave the pass in place with the Statistics screen matching. Confirm the leaderboard, if enabled, no longer lists the removed attempts, and that the essay posts for any essay question were deleted.
When to get help
This command is an afternoon including testing. Cohort workflows that reset quizzes on a schedule, archive the old results to a reporting table first, or notify learners afterwards are small projects for our LearnDash services team, and a LearnDash developer can also add a confirmation screen in the admin for staff who do not use the command line.
Frequently asked questions
Does this also reset the learner's course progress?
No. It removes quiz attempts and their related rows. A completed course or a ticked quiz step stays as it was unless you also clear course progress.
Can I reset only failed attempts?
Yes. The --failed-only flag skips attempts whose pass value is set, leaving passes intact.
Are quiz statistics deleted too?
Yes. learndash_remove_user_quiz_attempt() removes the statistics rows referenced by the attempt, the leaderboard row, the activity record and any essay posts.
Is there an admin screen for bulk resets?
Not in LearnDash itself. The profile offers per-user removal; the command above is the bulk equivalent.
Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.
