Writing a php script to delete all the courses in Moodle 2.7

Yesterday, I wrote a php script to trying to delete all the courses in my Moodle2.7 site. The script was based on the course/delete.php of moodle.

#!/usr/bin/php
<?php
define('CLI_SCRIPT', true);
require_once(dirname(__FILE__) . '/../config.php');
require_once($CFG->dirroot . '/course/lib.php');

$id = $argv[1];

$course = get_course($id);

delete_course($course);
?>

But after I ran it, errors showed up when I clicked to the category link of the deleted course. I ended up deleting that category to get rid of the error.

Trying to fix it today.


* Update Web June 4, 2014:

Add these following line to the above code:

...
delete_course($course);
fix_course_sortorder();
exit;
?>


root@mymoodle:/var/www/moodle/course# php bulk_delete.php 75

It works! So, the next thing to do is to get all the course id and delete them.

Here is the complete working script with improvement, copy it to /<moodle-root-path>/course/ to run:



Comments