diff --git a/classes/local/manager/process_manager.php b/classes/local/manager/process_manager.php
index fbc806f85b4223a8b92ecfdeb1ecc97c3923380a..f8b28c9fc9b7a16eced8bc6eb1e5f9efe940b451 100644
--- a/classes/local/manager/process_manager.php
+++ b/classes/local/manager/process_manager.php
@@ -230,10 +230,19 @@ class process_manager {
     public static function course_deletion_observed($event) {
         $process = self::get_process_by_course_id($event->get_data()['courseid']);
         if ($process) {
-            $step = step_manager::get_step_instance_by_workflow_index($process->workflowid, $process->stepindex);
-            $steplib = lib_manager::get_step_lib($step->subpluginname);
-            $steplib->abort_course($process);
-            self::remove_process($process);
+            self::abort_process($process);
         }
     }
+
+    /**
+     * Aborts a running process.
+     * @param process $process The process to abort.
+     * @throws \dml_exception
+     */
+    public static function abort_process($process) {
+        $step = step_manager::get_step_instance_by_workflow_index($process->workflowid, $process->stepindex);
+        $steplib = lib_manager::get_step_lib($step->subpluginname);
+        $steplib->abort_course($process);
+        self::remove_process($process);
+    }
 }
diff --git a/db/upgrade.php b/db/upgrade.php
index 1ee58e39902355f4db55431fcd2fa385058af038..6de05fb90af41c4494cc3a400754a72719dc4175 100644
--- a/db/upgrade.php
+++ b/db/upgrade.php
@@ -433,5 +433,18 @@ function xmldb_tool_lifecycle_upgrade($oldversion) {
         upgrade_plugin_savepoint(true, 2019082300, 'tool', 'lifecycle');
     }
 
+    if ($oldversion < 2020091800) {
+        $sql = "SELECT p.* FROM {tool_lifecycle_process} p " .
+                "LEFT JOIN {course} c ON p.courseid = c.id " .
+                "WHERE c.id IS NULL";
+        $processes = $DB->get_records_sql($sql);
+        foreach ($processes as $procrecord) {
+            $process = \tool_lifecycle\local\entity\process::from_record($procrecord);
+            \tool_lifecycle\local\manager\process_manager::abort_process($process);
+        }
+
+        upgrade_plugin_savepoint(true, 2020091800, 'tool', 'lifecycle');
+    }
+
     return true;
 }
\ No newline at end of file
diff --git a/version.php b/version.php
index aaf19368ecca1a2236cadcc9c9223f216bcf8f47..947b0b6b96c9ff1de0c093f0f9a32f3560fc30af 100644
--- a/version.php
+++ b/version.php
@@ -25,7 +25,7 @@
 defined('MOODLE_INTERNAL') || die;
 
 $plugin->maturity = MATURITY_BETA;
-$plugin->version  = 2020060500;
+$plugin->version  = 2020091800;
 $plugin->component = 'tool_lifecycle';
 $plugin->requires = 2017111300; // Require Moodle 3.4 (or above).
 $plugin->release = 'v3.9-r1';