diff --git a/.upgradenotes/MDL-87173-2025111702594380.yml b/.upgradenotes/MDL-87173-2025111702594380.yml
new file mode 100644
index 00000000000..3e05478bf3b
--- /dev/null
+++ b/.upgradenotes/MDL-87173-2025111702594380.yml
@@ -0,0 +1,9 @@
+issueNumber: MDL-87173
+notes:
+ core:
+ - message: >-
+ The CLI script used to terminate user sessions (kill_all_sessions.php)
+ has been improved to make it safer and more flexible.
+ A new '--run' parameter has been introduced. Without '--run', the script performs a dry run making no changes.
+ The script now supports targeted session termination using '--for-users' parameter.
+ type: improved
diff --git a/admin/cli/kill_all_sessions.php b/admin/cli/kill_all_sessions.php
index de0e71d3430..65cd4121842 100644
--- a/admin/cli/kill_all_sessions.php
+++ b/admin/cli/kill_all_sessions.php
@@ -15,7 +15,7 @@
// along with Moodle. If not, see .
/**
- * CLI script to kill all user sessions without asking for confirmation.
+ * CLI script to kill user sessions.
*
* @package core
* @subpackage cli
@@ -28,28 +28,59 @@ define('CLI_SCRIPT', true);
require(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/clilib.php');
-list($options, $unrecognized) = cli_get_params(array('help' => false), array('h' => 'help'));
+[$options, $unrecognized] = cli_get_params([
+ 'help' => false,
+ 'run' => false,
+ 'for-users' => false,
+], [
+ 'h' => 'help',
+]);
if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized), 2);
}
-if ($options['help']) {
- $help =
-"Kill all Moodle sessions
+if ($options['help'] || !empty($options['for-users']) && !is_string($options['for-users'])) {
+ $help = <<