Removal of Scheduled Task Definitions

Removal of Scheduled Task Definitions

by Michael Hughes -
Number of replies: 2
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

I have a custom plugin that in the past defined a scheduled task, which I now want to remove (as the task is no longer required).

I was just wanting to check the anticipated / correct way for removal.

My gut reaction is that I could do in /db/upgrade.php in xmldb_local_xxxx_upgrade() a $DB->delete_record('task_scheduled', array('module'=>'local_strath',  'classname'=>\local_strath\task\get_token);

I'm correct in thinking that there isn't anything that uninstalls (e.g. in the *opposite* manner that the definition on db/tasks.php works) tasks?

Average of ratings: -
In reply to Michael Hughes

Re: Removal of Scheduled Task Definitions

by Darko Miletić -

You are correct in your assumption that there is no specific API call to uninstall task no longer required. The right way to do this would be to add the removal code to the db/upgrade.php of the plugin like this:

function xmldb_myplugintype_pluginname_upgrade($oldversion = 0) {
    global $DB;

    if ($oldversion < 1234567889) {
        // Delete scheduled tasks
        $DB->delete_records('task_scheduled', array('component' => 'myplugincomponent'));

        upgrade_plugin_savepoint(true, 1234567889, $plugintype, $pluginname);
    }

    return true;
}


In reply to Michael Hughes

Re: Removal of Scheduled Task Definitions

by Frédéric Massart ⭐ -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Michael,

Removing the task from db/tasks.php and bumping the version of your plugin should be enough. During upgrade Moodle will remove the obsolete tasks and add the new ones.

Cheers,

Fred

PS: Since Moodle 2.8.8 and 2.9.2 -> MDL-50532