protected function cancelExistingJobs($type)
    {
        $clearable = array(
            // Paused jobs need to be discarded
            QueuedJob::STATUS_PAUSED,

            // These types would be automatically started
            QueuedJob::STATUS_NEW,
            QueuedJob::STATUS_WAIT,

            // Cancel any in-progress job
            QueuedJob::STATUS_INIT,
            QueuedJob::STATUS_RUN
        );
        DB::query(sprintf(
            'UPDATE "%s" '
                . ' SET "JobStatus" = \'%s\''
                . ' WHERE "JobStatus" IN (\'%s\')'
                . ' AND "Implementation" = \'%s\'',
            Convert::raw2sql(DataObject::getSchema()->tableName(QueuedJobDescriptor::class)),
            Convert::raw2sql(QueuedJob::STATUS_CANCELLED),
            implode("','", Convert::raw2sql($clearable)),
            Convert::raw2sql($type)
        ));
        return DB::affected_rows();
    }