# CLI scripts

<div class="mw-content-ltr mw-parser-output" dir="ltr" id="bkmrk-installation-install" lang="en"><div class="sideblock right" style="width: 16em;"><div class="header">[Installation](/503/en/Installation "Installation")</div><div class="content">

- [Installing Moodle](/503/en/Installing_Moodle "Installing Moodle")
- [Installation quick guide](/503/en/Installation_quick_guide "Installation quick guide")
- [Cron](/503/en/Cron "Cron")
- [Nginx](/503/en/Nginx "Nginx")
- [Apache](/503/en/Apache "Apache")
- [IIS](/503/en/Internet_Information_Services "Internet Information Services")
- [Routing](/503/en/Configuring_the_Router "Configuring the Router")
- [Installing plugins](/503/en/Installing_plugins "Installing plugins")
- [Installation FAQ](/503/en/Installation_FAQ "Installation FAQ")
- [Upgrading](/503/en/Upgrading "Upgrading")
- [Upgrade overview](/503/en/Upgrade_overview "Upgrade overview")
- [Automatic updates deployment](/503/en/Automatic_updates_deployment "Automatic updates deployment")
- [Git guide](/503/en/Git_for_Administrators "Git for Administrators")
- <a class="mw-selflink selflink">Administration via command line</a>
- [Upgrading FAQ](/503/en/Upgrading_FAQ "Upgrading FAQ")
- [Moodle migration](/503/en/Moodle_migration "Moodle migration")

</div></div>

- [<span class="tocnumber">1</span> <span class="toctext">Running CLI scripts</span>](#bkmrk-running-cli-scripts)
- [<span class="tocnumber">2</span> <span class="toctext">The bin/moodle command line tool</span>](#bkmrk-the-bin%2Fmoodle-comma-1)
    - [<span class="tocnumber">2.1</span> <span class="toctext">Available commands</span>](#bkmrk-available-commands)
- [<span class="tocnumber">3</span> <span class="toctext">Upgrading</span>](#bkmrk-upgrading)
    - [<span class="tocnumber">3.1</span> <span class="toctext">PHP Extra Memory</span>](#bkmrk-php-extra-memory)
- [<span class="tocnumber">4</span> <span class="toctext">Installation</span>](#bkmrk-installation)
- [<span class="tocnumber">5</span> <span class="toctext">Maintenance mode</span>](#bkmrk-maintenance-mode)
- [<span class="tocnumber">6</span> <span class="toctext">Offline mode</span>](#bkmrk-offline-mode)
- [<span class="tocnumber">7</span> <span class="toctext">Custom site defaults</span>](#bkmrk-custom-site-defaults-1)
- [<span class="tocnumber">8</span> <span class="toctext">Reset user password</span>](#bkmrk-reset-user-password)
- [<span class="tocnumber">9</span> <span class="toctext">Display manual login form</span>](#bkmrk-display-manual-login-1)
- [<span class="tocnumber">10</span> <span class="toctext">Converting InnoDB to Barracuda</span>](#bkmrk-converting-innodb-to-1)
    - [<span class="tocnumber">10.1</span> <span class="toctext">Changing tables to compressed row format</span>](#bkmrk-changing-tables-to-c-1)
    - [<span class="tocnumber">10.2</span> <span class="toctext">Converting character set and collation</span>](#bkmrk-converting-character-1)
- [<span class="tocnumber">11</span> <span class="toctext">Running cron via command line</span>](#bkmrk-running-cron-via-com-1)
- [<span class="tocnumber">12</span> <span class="toctext">Scheduled tasks</span>](#bkmrk-scheduled-tasks)
    - [<span class="tocnumber">12.1</span> <span class="toctext">Ad hoc tasks</span>](#bkmrk-ad-hoc-tasks)
- [<span class="tocnumber">13</span> <span class="toctext">Database transfer</span>](#bkmrk-database-transfer)
- [<span class="tocnumber">14</span> <span class="toctext">Purge caches</span>](#bkmrk-purge-caches)
- [<span class="tocnumber">15</span> <span class="toctext">Kill all sessions</span>](#bkmrk-kill-all-sessions)
- [<span class="tocnumber">16</span> <span class="toctext">Backup, restore and import of courses or modules</span>](#bkmrk-backup%2C-restore-and--1)
- [<span class="tocnumber">17</span> <span class="toctext">Deleting courses</span>](#bkmrk-deleting-courses)
- [<span class="tocnumber">18</span> <span class="toctext">Fix course / module sequences</span>](#bkmrk-fix-course-%2F-module--1)
- [<span class="tocnumber">19</span> <span class="toctext">Fix orphaned question categories</span>](#bkmrk-fix-orphaned-questio-1)
- [<span class="tocnumber">20</span> <span class="toctext">Search and replace text</span>](#bkmrk-search-and-replace-t-1)
- [<span class="tocnumber">21</span> <span class="toctext">Build theme CSS cache</span>](#bkmrk-build-theme-css-cach-1)
- [<span class="tocnumber">22</span> <span class="toctext">Get and set configuration values</span>](#bkmrk-get-and-set-configur-1)
- [<span class="tocnumber">23</span> <span class="toctext">E-mail flag for users</span>](#bkmrk-e-mail-flag-for-user-1)
- [<span class="tocnumber">24</span> <span class="toctext">Dashboard reset</span>](#bkmrk-dashboard-reset)
- [<span class="tocnumber">25</span> <span class="toctext">Managing plugins via command line</span>](#bkmrk-managing-plugins-via-1)
    - [<span class="tocnumber">25.1</span> <span class="toctext">List and uninstall Moodle plugins</span>](#bkmrk-list-and-uninstall-m-1)
- [<span class="tocnumber">26</span> <span class="toctext">See also</span>](#bkmrk-see-also)

</div>## <span class="mw-headline" id="bkmrk-running-cli-scripts">Running CLI scripts</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

If you have shell access to your web server, you may find various CLI (command line interface) scripts useful during Moodle administration. Core admin CLI tools are located in the `admin/cli/*` folder. Other plugins may provide CLI functionality via scripts in their own `cli` folder.

To avoid problems with access control, you should run them as the owner of the web server process. It is especially important for CLI installation and upgrade as they create new files in moodledata directory and the web server has to have write access to them. In Linux distributions, the user that runs the web server is usually apache or www-data or httpd or something similar. As a root, you will probably want to execute Moodle CLI scripts like this:

```
   $ cd /path/to/your/moodle/dir
   $ sudo -u apache /usr/bin/php admin/cli/somescript.php --params
```

Most of the scripts accept common --help (or -h) parameter to display the full usage information, for example:

```
   $ sudo -u apache /usr/bin/php admin/cli/install.php --help
```

<div class="alert alert-info" id="bkmrk-note%3A-these-scripts-">**Note:** These scripts are supposed to be run under the identity of the web server user. Examples on this page use the <tt>apache</tt> user for illustration. The particular value depends on your OS distribution and local set-up. Typical values may be <tt>apache</tt>, <tt>www-data</tt> or <tt>httpd</tt>.</div>## <span class="mw-headline" id="bkmrk-the-bin%2Fmoodle-comma-1">The bin/moodle command line tool</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

<span class="small-info-right">[New feature  
in **Moodle 5.3**!](/503/en/Category:New_features "Category:New features")</span>

**From Moodle 5.3**, Moodle includes a new command line tool, `bin/moodle`, built on the Symfony Console component. It's an additional way to run some common administration tasks. The existing scripts in `admin/cli` are still available and fully supported.

<div class="alert alert-info" id="bkmrk-note%3A-bin%2Fmoodle-nee">**Note:** <code>bin&#47;moodle</code> needs the Composer dependencies to be installed. If they're missing, it stops with the message *Symfony console has not been installed via composer. Please run composer install.* See [Install Composer dependencies](/503/en/Installing_Moodle#Install_Composer_dependencies "Installing Moodle").</div>  
Run <code>bin&#47;moodle</code> from your Moodle root directory (the directory above <code>public&#47;</code> that contains config.php), as the web server user. To see all available commands:

```
   $ cd /path/to/your/moodle/dir
   $ sudo -u apache /usr/bin/php bin/moodle list
```

To see the options for a command, use `help` followed by the command name, or add `--help`:

```
   $ sudo -u apache /usr/bin/php bin/moodle help admin:plugins:uninstall
```

### <span class="mw-headline" id="bkmrk-available-commands">Available commands</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

<table class="wikitable" id="bkmrk-command-what-it-does"><tbody><tr><th>Command</th><th>What it does</th><th>Similar admin/cli script </th></tr><tr><td><code>admin&#58;cron</code></td><td>Runs cron. Options let you list running tasks, stop other cron processes, or enable and disable the cron runner.</td><td>cron.php </td></tr><tr><td><code>admin&#58;purge&#45;caches</code></td><td>Purges all caches, or only the types you choose (for example <code>&#45;&#45;theme</code>, <code>&#45;&#45;lang</code> or <code>&#45;&#45;courses&#61;4&#44;67</code>).</td><td>purge\_caches.php </td></tr><tr><td><code>admin&#58;session&#58;destroy&#58;all</code></td><td>Logs out all users by destroying every user session.</td><td>kill\_all\_sessions.php </td></tr><tr><td><code>admin&#58;plugins&#58;list</code></td><td>Lists installed plugins. Use <code>&#45;&#45;contrib</code> for additional plugins only, or <code>&#45;&#45;missing</code> for plugins missing from disk.</td><td>uninstall\_plugins.php --show-all </td></tr><tr><td><code>admin&#58;plugins&#58;uninstall</code></td><td>Uninstalls one or more plugins, for example <code>admin&#58;plugins&#58;uninstall local&#95;myplugin block&#95;myblock</code>. It lists the plugins and asks you to confirm. Use <code>&#45;&#45;assume&#45;yes</code> to skip the confirmation. In non-interactive mode, <code>&#45;&#45;assume&#45;yes</code> is required.</td><td>uninstall\_plugins.php --plugins </td></tr><tr><td><code>admin&#58;plugins&#58;purge&#45;missing</code></td><td>Uninstalls every plugin whose files are missing from disk.</td><td>uninstall\_plugins.php --purge-missing </td></tr></tbody></table>

`admin:cron` can also be run as `cron`, and `admin:purge-caches` as `purge-caches`.

<table class="alert alert-warning" id="bkmrk-warning%3A-unlike-kill"><tbody><tr><td style="padding: 0.7em 0.5em 0.5em;">**Warning:** Unlike kill\_all\_sessions.php and uninstall\_plugins.php, <code>admin&#58;session&#58;destroy&#58;all</code> and <code>admin&#58;plugins&#58;purge&#45;missing</code> have no dry run and don't ask for confirmation. They make their changes as soon as you run them. </td></tr></tbody></table>

After using either plugin command, [update the Moodle database](#bkmrk-upgrading). `admin:plugins:uninstall` doesn't remove the plugin's files, so remove them first. Otherwise Moodle offers to install the plugin again.

## <span class="mw-headline" id="bkmrk-upgrading">Upgrading</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

Moodle can be upgraded from the command line. As with the installation script, there is either interactive or non-interactive mode of the upgrade. The script itself does not put the site into the maintenance mode, you have to do it on your own. Also, the script does not backup any data.

<div class="alert alert-info" id="bkmrk-note%3A-as-mentioned-a">**Note:** As mentioned above, the upgrade php script does not perform a backup of the Moodle site data. Before running this script, make sure you have taken a proper site backup including the software, data directory, and database before you perform the upgrade.</div>To run the interactive upgrade, execute the following command:

```
   $ sudo -u apache /usr/bin/php admin/cli/upgrade.php
```

Upgrading via command line is a very comfortable way of Moodle upgrade if you use Git checkout of the Moodle source code (see [Git for Administrators](/503/en/Git_for_Administrators "Git for Administrators")). See the following procedure how to upgrade your site within several seconds to the most recent version while preserving your eventual local customizations tracked in git repository:

```
   $ cd /var/www/sites/moodle/htdocs/
   $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable
   $ git pull
   $ sudo -u apache /usr/bin/php admin/cli/upgrade.php
   $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --disable
```

Alternatively, instead of using git, the updated Moodle package can be downloaded from the Moodle site using the wget command, and then extracted with the tar command.

```
   $ wget https://download.moodle.org/download.php/direct/stable405/moodle-4.5.1.tgz
   $ tar -xvzf moodle-4.5.1.tgz
```

Note that the path must contain "/direct/", or else wget will not work.

#### <span class="mw-headline" id="bkmrk-php-extra-memory">PHP Extra Memory</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

Depending on your environment setup, to perform a CLI upgrade you \*may\* need to run PHP explicitly with more memory.  
This is only required if PHP is failing due to out of memory conditions.

For example:

```
$ sudo -u apache /usr/bin/php --memory_limit=16G admin/cli/upgrade.php
```

The amount of memory will vary between Moodle LMS versions and your specific environment.

## <span class="mw-headline" id="bkmrk-installation">Installation</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

There are two modes of installing Moodle from the command line. In interactive mode, the install script asks you for all data needed to properly set up new Moodle site. In non-interactive mode, you must provide all required data as the script parameters and then the new site is installed silently. The parameters can be passed in the interactive mode, too. The provided values are then used as the default values during the interactive session.

```
   $ sudo -u apache /usr/bin/php admin/cli/install.php --lang=cs
```

If your arguments contain some specials characters for Linux-based systems, don't forget to *escape* them with a backslash. For example, if you want to create an admin with *pa$sword* as password you should wrote *pa\\$sword* instead!

If required, the database install may be skipped, with just config.php populated.

```
  $ sudo -u apache /usr/bin/php admin/cli/install.php --skip-database
```

If config.php already exists admin/cli/install\_database.php must be used instead, for example:

```
 $ sudo -u apache /usr/bin/php admin/cli/install_database.php --agree-license \
   --adminpass='secret' --fullname='My Site' --shortname='My Site' \
   --adminemail=admin@example.com --supportemail=support@example.com
```

## <span class="mw-headline" id="bkmrk-maintenance-mode">Maintenance mode</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

To switch your site into the maintenance mode via CLI, you can use

```
   $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable
```

To turn maintenance mode off, execute the same script with the --disable parameter:

```
   $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --disable
```

If you don't want to enable maintenance mode immediately, but show a countdown to your users, execute the same script with the --enablelater parameter and the number of minutes which the countdown should run:

```
   $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enablelater=10
```

This script will also create and remove the climaintenance.html file for "Offline" mode.

## <span class="mw-headline" id="bkmrk-offline-mode">Offline mode</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

In some situations, you may want to switch your Moodle site into offline mode so that it is not accessible via the web but you can not stop the web server completely (typically because there are other web pages and applications running there). If a file called `climaintenance.html` exists in the root folder of moodledata directory, Moodle will automatically display the contents of that file instead of any other page.

```
   $ cd /var/www/sites/moodle/moodledata/
   $ echo '<h1>Sorry, maintenance in progress</h1>' > climaintenance.html
```

You can prepare a nice formatted HTML page to inform your users about the server being down and keep in the moodledata directory under a name like `climaintenance.off` and rename it to the `climaintenance.html` if needed.

## <span class="mw-headline" id="bkmrk-custom-site-defaults-1">Custom site defaults</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

During the install and upgrade via CLI, Moodle sets the administration variables to the default values. You can use different defaults. See [MDL-17850](https://tracker.moodle.org/browse/MDL-17850) for details. Shortly, all you need to do is to add a file `local/defaults.php` into your Moodle installation. The format of the file is like

<div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr" id="bkmrk-%3C%3Fphp-%24defaults%5B%27plu">

```
<span class="cp"><?php</span>
<span class="nv">$defaults</span><span class="p">[</span><span class="s1">'pluginname'</span><span class="p">][</span><span class="s1">'settingname'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'settingvalue'</span><span class="p">;</span> <span class="c1">// for plugins</span>
<span class="nv">$defaults</span><span class="p">[</span><span class="s1">'moodle'</span><span class="p">][</span><span class="s1">'settingname'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'settingvalue'</span><span class="p">;</span>     <span class="c1">// for core settings</span>
```

</div>These defaults are used during install, upgrade and are also displayed as defaults on Site administration pages.

## <span class="mw-headline" id="bkmrk-reset-user-password">Reset user password</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

If you happen to forget your admin password (or you want to set a password for any other user on the site), you can use reset\_password.php script. The script sets the correctly salted password for the given user.

```
   $ sudo -u apache /usr/bin/php admin/cli/reset_password.php
```

## <span class="mw-headline" id="bkmrk-display-manual-login-1">Display manual login form</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

If you hide the manual login form then find that you can no longer log in, you can display it again using the command line

```
php admin/cli/cfg.php --name=showloginform --set=1
```

## <span class="mw-headline" id="bkmrk-converting-innodb-to-1">Converting InnoDB to Barracuda</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

Newer versions of MySQL and MariaDB have an improved file format called Barracuda. To take advantage of this:

1. Ensure your version of MySQL/MariaDB is using Barracuda with the large index key prefix (innodb\_large\_prefix). See below for details.
2. Change to the compressed row format (see [Changing tables to compressed row format](#bkmrk-changing-tables-to-c-1))
3. Change the character set and collation for full Unicode support (see [Converting character set and collation](#bkmrk-converting-character-1)).

  
If you have MySQL 8.0.0 or later, or MariaDB 10.3.0 or later, then these versions only support Barracuda with the large index key prefix. For earlier versions to check if MySQL/MariaDB is using Barracuda run the following statement in the MySQL/MariaDB client or phpMyAdmin SQL tab:

```
SHOW GLOBAL VARIABLES WHERE variable_name IN ('innodb_file_format', 'innodb_large_prefix', 'innodb_file_per_table');
```

If your settings do not match any of the tables below, see [Configure full UTF-8 support](/503/en/MySQL#Configure_full_UTF-8_support "MySQL") for how to change these settings:

<span class="mw-default-size" typeof="mw:File">[![innodb&#95;file&#95;format=Barracuda;innodb&#95;file&#95;per&#95;table=ON;innodb&#95;large&#95;prefix=ON](https://docs.moodle.org/503/en/images_en/c/ca/phpMyAdmin1.png)](/503/en/File:phpMyAdmin1.png)</span> or <span class="mw-default-size" typeof="mw:File">[![innodb&#95;file&#95;format=(blank);innodb&#95;file&#95;per&#95;table=ON;innodb&#95;large&#95;prefix=(blank)](https://docs.moodle.org/503/en/images_en/d/da/phpMyAdmin2.png)](/503/en/File:phpMyAdmin2.png)</span> or <span class="mw-default-size" typeof="mw:File">[![innodb&#95;file&#95;per&#95;table=ON](https://docs.moodle.org/503/en/images_en/6/6b/phpMyAdmin3.png)](/503/en/File:phpMyAdmin3.png)</span>

If your settings match one of these tables then MySQL/MariaDB is using Barracuda with the large index key prefix.

### <span class="mw-headline" id="bkmrk-changing-tables-to-c-1">Changing tables to compressed row format</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

This script changes tables with many text columns to use ROW\_FORMAT=COMPRESSED. This change isn't required but is recommended. If the database error *Row size too large (&gt; 8126)* occurs when using Moodle then run this script to fix this.

**Note:** If you have MariaDB version 10.6.0 to 10.6.6 or version 10.7.0 to 10.7.2 you must upgrade to a later version before running this script. See [MDL-72131](https://tracker.moodle.org/browse/MDL-72131) for details.

To view tables requiring conversion, use the --list option:

```
$ php admin/cli/mysql_compressed_rows.php --list
```

Here is an example output:

```
mdl_data                            Compact     (needs fixing) 
mdl_data_fields                     Compact     (needs fixing)
mdl_enrol_paypal                    Compact     (needs fixing)
```

To proceed with the conversion, run the command using the fix option:

```
$ php admin/cli/mysql_compressed_rows.php --fix
```

Successful table conversion will be reported in the output, for example:

```
mdl_data                   ... Compressed
mdl_data_fields            ... Compressed
mdl_enrol_paypal           ... Compressed
```

If you get errors due to insufficient privileges to run these commands use --showsql to generate the required SQL commands:

```
$ php admin/cli/mysql_compressed_rows.php --showsql
```

You can then copy the generated SQL into your MySQL/MariaDB client running as the 'root' user.

### <span class="mw-headline" id="bkmrk-converting-character-1">Converting character set and collation</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

This script changes the Moodle database tables to enable full Unicode support.

```
$ php admin/cli/mysql_collation.php --collation=utf8mb4_unicode_ci
```

After making this change edit config.php and change the 'dbcollation' to 'utf8mb4\_unicode\_ci' in the $CFG-&gt;dboptions array.

For further details see: [MySQL full unicode support](/503/en/MySQL_full_unicode_support "MySQL full unicode support")

## <span class="mw-headline" id="bkmrk-running-cron-via-com-1">Running cron via command line</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

In versions 1.x, you could execute admin/cron.php either from command line or via the web. Since Moodle 2.0, only admin/cli/cron.php script can be run via command line.

## <span class="mw-headline" id="bkmrk-scheduled-tasks">Scheduled tasks</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

Scheduled tasks are automatically run by the cron script, but the specific tasks which run on each cron iteration are determined by the scheduled tasks configuration. It is possible to override the scheduled tasks configuration and run a single scheduled task, or all due scheduled tasks, immediately using the admin/cli/scheduled\_task.php script.

This script accepts the following arguments:

```
--list - list all the known scheduled tasks. The tasks are listed by the class name used to run the task. This class name is required as the argument to the next option in order to run a specific task immediately.
```

```
-e, --execute - <b>From Moodle 5.3</b>, runs all due scheduled tasks immediately, regardless of scheduling settings, matching the existing behaviour of admin/cli/adhoc_task.php --execute.
```

```
-e, --execute=<task> - Runs a single scheduled task immediately - regardless of scheduling settings. This will even run disabled tasks. Tasks will still use locking to prevent concurrent execution of the same task - even on clusters. The format of the <task> argument must be the same as returned by the --list option above.
```

```
--disable=<task> - Disables a single scheduled task. The format of the <task> argument must be the same as returned by the --list option above.
```

```
--enable=<task> - Enables a single scheduled task. The format of the <task> argument must be the same as returned by the --list option above.
```

```
-f, --force - <b>From Moodle 5.3</b>, runs the task even if cron is disabled site-wide.
```

```
--showsql - Shows sql queries before they are execute
```

```
--showdebugging - Shows developer debugging info
```

**Note:** You must escape the "\\" with an extra \\ when using the --execute command. Take the following for example:

```
php admin/cli/scheduled_task.php --list
```

will return something like:

```
== List of scheduled tasks (http://yourserver.com/moodle) ==

\enrol_imsenterprise\task\cron_task                10 * * * * *      ASAP
\logstore_legacy\task\cleanup_task                 * 5 * * * *       ASAP
\logstore_standard\task\cleanup_task               * 4 * * * *       Wednesday, November 12, 2014, 4:35 AM
\mod_forum\task\cron_task                          * * * * * *       ASAP
\core\task\automated_backup_task                   50 * * * * *      ASAP

...
```

To run all due scheduled tasks:

```
php admin/cli/scheduled_task.php --execute
```

To run a specific task from that list:

```
php admin/cli/scheduled_task.php --execute='\enrol_imsenterprise\task\cron_task'
```

Be aware of the single quotes. Without them, you would need to use double backlashes to avoid escaping by the shell.

### <span class="mw-headline" id="bkmrk-ad-hoc-tasks">Ad hoc tasks</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

[Ad hoc tasks](/503/en/Cron#Low_latency_adhoc_tasks "Cron") are low-latency tasks that are normally run by the cron. You can run just the queued ad hoc tasks with admin/cli/adhoc\_task.php.

This script accepts the following options and arguments:

```
 -h, --help                Print out this help
     --showsql             Show sql queries before they are executed
     --showdebugging       Show developer level debugging information
 -e, --execute             Run all queued adhoc tasks
 -k, --keep-alive=N        Keep this script alive for N seconds and poll for new adhoc tasks
 -i  --ignorelimits        Ignore task_adhoc_concurrency_limit and task_adhoc_max_runtime limits
 -f, --force               Run even if cron is disabled
```

For example to run the currently queued ad hoc tasks:

```
sudo -u apache php admin/cli/adhoc_task.php -e
```

See also the improvements to ad hoc tasks in [Scheduled\_tasks#Ad\_hoc\_tasks](/503/en/Scheduled_tasks#Ad_hoc_tasks "Scheduled tasks")

## <span class="mw-headline" id="bkmrk-database-transfer">Database transfer</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

A command line script for [Database transfer](/503/en/Database_transfer "Database transfer") may be found in *admin/tool/dbtransfer/cli/migrate.php*.

## <span class="mw-headline" id="bkmrk-purge-caches">Purge caches</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

You can purge caches using this script:

```
 php admin/cli/purge_caches.php
```

**From Moodle 5.3**, you can also use `bin/moodle admin:purge-caches`. See [The bin/moodle command line tool](#bkmrk-the-bin%2Fmoodle-comma-1).

## <span class="mw-headline" id="bkmrk-kill-all-sessions">Kill all sessions</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

If needed for administrative reasons, you can kill all user sessions using this script:

```
php admin/cli/kill_all_sessions.php --run
```

As a result, all users will be logged out from Moodle.

**From Moodle 5.2**, the script supports targeted session termination and runs in dry mode by default.

To terminate sessions for specific users only, pass a comma-separated list of user IDs with `--for-users`:

```
php admin/cli/kill_all_sessions.php --for-users=2,3,4 --run
```

Without `--run`, the script runs in dry mode — it reports what would happen without making any changes. This is useful for verifying which sessions will be terminated before executing.

Options:

- `--for-users=<id,id,...>` — Comma-separated list of user IDs whose sessions should be terminated. If omitted, all sessions are terminated.
- `--run` — Execute the termination. Without this option, the script runs in dry mode.

**From Moodle 5.3**, you can also use `bin/moodle admin:session:destroy:all`, which has no dry run. See [The bin/moodle command line tool](#bkmrk-the-bin%2Fmoodle-comma-1).

## <span class="mw-headline" id="bkmrk-backup%2C-restore-and--1">Backup, restore and import of courses or modules</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

See Backup via CLI in [Course backup](/503/en/Course_backup "Course backup") and Restore via CLI in [Course restore](/503/en/Course_restore "Course restore") (**in 3.10 onwards**). Courses or individual modules can be copied directly on the command line with import.php (**in 4.4 onwards**). See Import via CLI in [Course copy](/503/en/Course_copy "Course copy").

## <span class="mw-headline" id="bkmrk-deleting-courses">Deleting courses</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

Courses can be deleted via CLI if you know the course id:

```
sudo -u www-data /usr/bin/php admin/cli/delete_course.php --courseid=123456
```

**From Moodle 5.2**, courses can also be deleted by shortname or id number:

```
sudo -u www-data /usr/bin/php admin/cli/delete_course.php --shortname=mycourseshortname
sudo -u www-data /usr/bin/php admin/cli/delete_course.php --idnumber=COURSE001
```

## <span class="mw-headline" id="bkmrk-fix-course-%2F-module--1">Fix course / module sequences</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

In rare cases (such as after upgrading from a very old version of Moodle), the course / section / module sequence data can be out of sync. This can cause various problems for affected courses, such as sections not appearing, backups failing, pages not displaying etc. There is a specific check to check for errors caused by this problem, and to fix the data in the database if they are found. To run this script please use the command below:

```
 php admin/cli/fix_course_sequence.php -c=* --fix
```

This will check every course in Moodle and report which ones had errors and were fixed.

## <span class="mw-headline" id="bkmrk-fix-orphaned-questio-1">Fix orphaned question categories</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

When a quiz is created, a new question category for the quiz is automatically created. In versions of Moodle prior to 2.9.1, if the quiz is deleted, the question category and any questions in the category remain in database. These orphaned question categories may be fixed by running the admin/cli/fix\_orphaned\_question\_categories.php script with the --fix option.

## <span class="mw-headline" id="bkmrk-search-and-replace-t-1">Search and replace text</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

This script can be used to search and replace text throughout the whole database. Use carefully and backup first always. More info in [Search and replace tool](/503/en/Search_and_replace_tool "Search and replace tool").

```
 php admin/tool/replace/cli/replace.php --search=//oldsitehost --replace=//newsitehost
```

## <span class="mw-headline" id="bkmrk-build-theme-css-cach-1">Build theme CSS cache</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

If Moodle is not running in theme designer mode it will keep a copy of the compiled CSS on local disk and serve that to the browser when it requests a page. If there isn't a copy on local disk then a copy will be built the first time a page within Moodle is requested.

With this script you can pre-compile the cached CSS files for themes within Moodle to avoid having a user wait for the theme to compile during the first page request.

```
 php admin/cli/build_theme_css.php --themes=boost
```

## <span class="mw-headline" id="bkmrk-get-and-set-configur-1">Get and set configuration values</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

Displays the current value of the given setting, or set the given setting to the specified value.

```
$ php admin/cli/cfg.php [--component=<componentname>] [--json] [--shell-arg]
$ php admin/cli/cfg.php --name=<configname> [--component=<componentname>] [--shell-arg] [--no-eol]
$ php admin/cli/cfg.php --name=<configname> [--component=<componentname>] --set=<value>
$ php admin/cli/cfg.php --name=<configname> [--component=<componentname>] --unset
$ php admin/cli/cfg.php [--help|-h]
```

Examples:

```
$ php admin/cli/cfg.php --name=langmenu
```

will display the value of *Display language menu* in *Site administration &gt; Language &gt; Language settings* (0 for *No* or 1 for *Yes*).

```
$ php cfg.php --name=maxsizetodownload --component=folder
```

will display the value of *Maximum folder download size (MB)* accessible from *Site administration &gt; Plugins &gt; Activity modules &gt; Folder*.

```
$ php admin/cli/cfg.php --name=langmenu --set=0
```

will disable the *Language menu*.

## <span class="mw-headline" id="bkmrk-e-mail-flag-for-user-1">E-mail flag for users</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

**From Moodle 4.2**, the CLI script to change the email flag for one or many users at a time can be found on the page [E-Mail flag for users](/503/en/E-Mail_flag_for_users "E-Mail flag for users")

## <span class="mw-headline" id="bkmrk-dashboard-reset">Dashboard reset</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

If you make changes to the dashboard and want all users to have their dashboard reset to reflect the changes this can be done on the cli:

```
php admin/cli/dashboard_reset.php -e
```

## <span class="mw-headline" id="bkmrk-managing-plugins-via-1">Managing plugins via command line</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

### <span class="mw-headline" id="bkmrk-list-and-uninstall-m-1">List and uninstall Moodle plugins</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

Moodle includes a handy command-line tool to display a list of plugins and remove plugins.

```
$ php admin/cli/uninstall_plugins.php
```

This command line tool accepts the following arguments.

Options:

```
    -h --help                   Print this help.
    --show-all                  Displays a list of all installed plugins.
    --show-contrib              Displays a list of all third-party installed plugins.
    --show-missing              Displays a list of plugins missing from disk.
    --purge-missing             Uninstall all missing from disk plugins.
    --plugins=<plugin name>     A comma separated list of plugins to be uninstalled. E.g. mod_assign,mod_forum
    --run                       Execute uninstall. If this option is not set, then the script will be run in a dry mode.
    --showsql                   Show sql queries before they are executed.
    --showdebugging             Show developer level debugging information.
```

Dry Mode does everything the script would normally do except for actually making any changes. This is useful when you want to test the validity of your command.

Run this command from the Moodle wwwroot directory. Here are some examples:

```
    # php admin/cli/uninstall_plugins.php  --show-all
       Prints tab-separated list of all installed plugins.
    # php admin/cli/uninstall_plugins.php  --show-contrib
       Prints tab-separated list of all third-party installed plugins.
    # php admin/cli/uninstall_plugins.php  --show-missing
```

> Prints tab-separated list of all missing from disk plugins.

```
    # php admin/cli/uninstall_plugins.php  --purge-missing
       A dry run of uninstalling all missing plugins. This will NOT uninstall the plugin. To actually uninstall, you need to add the '--run' arguments.
    # php admin/cli/uninstall_plugins.php  --purge-missing --run
       Run uninstall of all missing plugins.
    # php admin/cli/uninstall_plugins.php  --plugins=mod_assign,mod_forum
       A dry run of uninstalling mod_assign and mod_forum plugins. This will NOT uninstall the plugin. To actually uninstall, you need to add the '--run' arguments.
    # php admin/cli/uninstall_plugins.php  --plugins=mod_assign,mod_forum --run
       Run uninstall for mod_assign and mod_forum plugins.
```

This tool does not perform all of the steps needed to fully uninstall a plugin. You must then:

- Remove the files belonging to the plugin manually;
- [Update the Moodle database](#bkmrk-upgrading) as previously described.

If you skip one or both of these steps, you will be prompted to update the Moodle database or re-install the plugin the next time you log in as a site administrator and access the Site Administration &gt; Notifications page.

**From Moodle 5.3**, you can also manage plugins with `bin/moodle admin:plugins:list`, `admin:plugins:uninstall` and `admin:plugins:purge-missing`. See [The bin/moodle command line tool](#bkmrk-the-bin%2Fmoodle-comma-1).

## <span class="mw-headline" id="bkmrk-see-also">See also</span><span class="mw-editsection-divider"> \| </span><span class="mw-editsection-bracket">\]</span>

- [MDL-35736](https://tracker.moodle.org/browse/MDL-35736) - Manage plugins via command-line
- [MDL-36237](https://tracker.moodle.org/browse/MDL-36237) - Resort course list via CLI
- [MOOSH](http://moosh-online.com/) - MOOdle SHell. It is a command-line tool that will allow you to perform many of the most common Moodle tasks.
- MOOSH 2 - for moodle core versions 5.1 and above. See link moosh2 link off the MOOSH link above.

---

<small>Source: [docs.moodle.org/503/en/CLI scripts](https://docs.moodle.org/503/en/CLI%20scripts) — MoodleDocs (GPL) · Imported 2026-11-10.</small>