Google Cloud IoT Core is being retired on August 16, 2023. Contact your Google Cloud account team for more information.

Detach a device

Stay organized with collections Save and categorize content based on your preferences.

Detach a device from a gateway.

Code sample


For more information, see the Cloud IoT Core Go API reference documentation.

// detatchDevice detaches a device from a gateway.
func detachDevice(deviceID string, client mqtt.Client, jwt string) error {
	detachTopic := fmt.Sprintf("/devices/%s/detach", deviceID)
	fmt.Printf("Detaching device: %s\n", detachTopic)

	detachPayload := "{}"
	if jwt != "" {
		detachPayload = fmt.Sprintf("{ 'authorization' : %s }", jwt)

	if token := client.Publish(detachTopic, 1, false, detachPayload); token.Wait() && token.Error() != nil {
		return token.Error()
	return nil


For more information, see the Cloud IoT Core Java API reference documentation.

final String detachTopic = String.format("/devices/%s/detach", deviceId);
System.out.println(String.format("Detaching: %s", detachTopic));
String attachPayload = "{}";
MqttMessage message = new MqttMessage(attachPayload.getBytes(;
client.publish(detachTopic, message);


For more information, see the Cloud IoT Core Python API reference documentation.

detach_topic = "/devices/{}/detach".format(device_id)
print("Detaching: {}".format(detach_topic))
client.publish(detach_topic, "{}", qos=1)

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.