public function deleteToolConsumer($consumer) {
        global $DB;

        $consumerpk = $consumer->getRecordId();
        $deletecondition = ['consumerid' => $consumerpk];

        // Delete any nonce values for this consumer.
        $DB->delete_records($this->noncetable, $deletecondition);

        // Delete any outstanding share keys for resource links for this consumer.
        $where = "resourcelinkid IN (
                      SELECT rl.id
                        FROM {{$this->resourcelinktable}} rl
                       WHERE rl.consumerid = :consumerid
                  )";
        $DB->delete_records_select($this->sharekeytable, $where, $deletecondition);

        // Delete any outstanding share keys for resource links for contexts in this consumer.
        $where = "resourcelinkid IN (
                      SELECT rl.id
                        FROM {{$this->resourcelinktable}} rl
                  INNER JOIN {{$this->contexttable}} c
                          ON rl.contextid = c.id
                       WHERE c.consumerid = :consumerid
                )";
        $DB->delete_records_select($this->sharekeytable, $where, $deletecondition);

        // Delete any users in resource links for this consumer.
        $where = "resourcelinkid IN (
                    SELECT rl.id
                      FROM {{$this->resourcelinktable}} rl
                     WHERE rl.consumerid = :consumerid
                )";
        $DB->delete_records_select($this->userresulttable, $where, $deletecondition);

        // Delete any users in resource links for contexts in this consumer.
        $where = "resourcelinkid IN (
                         SELECT rl.id
                           FROM {{$this->resourcelinktable}} rl
                     INNER JOIN {{$this->contexttable}} c
                             ON rl.contextid = c.id
                          WHERE c.consumerid = :consumerid
                )";
        $DB->delete_records_select($this->userresulttable, $where, $deletecondition);

        // Update any resource links for which this consumer is acting as a primary resource link.
        $where = "primaryresourcelinkid IN (
                    SELECT rl.id
                      FROM {{$this->resourcelinktable}} rl
                     WHERE rl.consumerid = :consumerid
                )";
        $updaterecords = $DB->get_records_select($this->resourcelinktable, $where, $deletecondition);
        foreach ($updaterecords as $record) {
            $record->primaryresourcelinkid = null;
            $record->shareapproved = null;
            $DB->update_record($this->resourcelinktable, $record);
        }

        // Update any resource links for contexts in which this consumer is acting as a primary resource link.
        $where = "primaryresourcelinkid IN (
                        SELECT rl.id
                          FROM {{$this->resourcelinktable}} rl
                    INNER JOIN {{$this->contexttable}} c
                            ON rl.contextid = c.id
                         WHERE c.consumerid = :consumerid
                )";
        $updaterecords = $DB->get_records_select($this->resourcelinktable, $where, $deletecondition);
        foreach ($updaterecords as $record) {
            $record->primaryresourcelinkid = null;
            $record->shareapproved = null;
            $DB->update_record($this->resourcelinktable, $record);
        }

        // Delete any resource links for contexts in this consumer.
        $where = "contextid IN (
                      SELECT c.id
                        FROM {{$this->contexttable}} c
                       WHERE c.consumerid = :consumerid
                )";
        $DB->delete_records_select($this->resourcelinktable, $where, $deletecondition);

        // Delete any resource links for this consumer.
        $DB->delete_records($this->resourcelinktable, $deletecondition);

        // Delete any contexts for this consumer.
        $DB->delete_records($this->contexttable, $deletecondition);

        // Delete consumer.
        $DB->delete_records($this->consumertable, ['id' => $consumerpk]);

        $consumer->initialize();

        return true;
    }