Moodle Course Delete From the Command Line(CLI)

Have you a lot of courses to delete within Moodle & tried doing it via the frontend ?

Quickly you will learn that this is a complete nightmare and may work & most probably wont!

So how do you go about deleting from the CLI on the server?

Create your .csv file, use Excel or other application

You only require a shortname and delete column

You can get the shortname like this from the database with this bit of SQL

Select shortname, '1' As 'delete' from mdl_course
Where shortname like '%1314%';

Export the output of this as a CSV and there is no need for Excel 😉

Now its time to jump on the server & head to

/var/www/moodle/docroot/admin/tool/uploaduser/cli

Create yourself a Screen session with something like

screen -S "RR #12345 1314 Course delete for XYZ"

And run something like this .. check your directory paths, don’t just trust me !

sudo -u www-data php uploaduser.php --mode=update --updatemode=dataonly --file=/var/www/moodle/tmp/All-1314-CoursesFrom-mdl_course.csv --delimiter=comma --allowdeletes |tee /var/www/moodle/tmp/deletecourses-1314_log.txt

Press CTRL + A + D to exit the screen session and come back in a few days, either by checking the log file you piped it out to or the Session itself to see if it has finished.

To note www-data is the group which your Moodle is running its commands as

Leave a comment