How to remove old / nonexistent Tag from Device Twin

Jeff Marcum 0 Reputation points
2025-01-10T18:08:00.7633333+00:00

I use IoT Hub to test MQTT connectivity with my company's hardware. I look at tag data I'm sending by using Device Twin (to verify the data was sent to Azure).

If I change the tags I'm sending - the Device Twin still shows old tags (with old data) that I've since removed from my device.

How can I get the Device Twin to show updated information; remove the old tags?

Thanks

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,234 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sander van de Velde | MVP 34,686 Reputation points MVP
    2025-01-10T23:36:30.5966667+00:00

    Hello @Jeff Marcum ,

    welcome to this moderated Azure community forum.

    You are referencing tags, device twins and data.

    It must be clear the Azure IoT Hub support MQTT as a communication protocol but only supports a limited set of MQTT topics with specific message formats so IoT Hub device-to-cloud and cloud-to-device communication takes place:

    • You can send telemetry message to the IoT Hub
    • You can receive desired properties (part of the device twin)
    • You can send reposted properties (part of the device twin) (this is an 'expensive' call and normally used as a reply on desired properties to tell the desired state is received)
    • Additionally, direct methods can be handled when the device is live connected

    A device also supports tags.

    These must be seen as context to a device registration (on the cloud side) so incoming messages can be enriched with these tags. These tags can also be used in a more special IoT Edge solution with layered deployments.

    The device cannot reach out to the tags.

    You are talking about tags but I think these are reported properties?

    These reported properties are normally not deleted because it represents the current state. If a device has certain reported properties and new desired properties are supplied, You can check if the device has picked up the changes already.

    Also, the Azure portal experience for the device twin is not automatically refreshed.

    You need to close and open the page again to refresh it.

    It's better to route device twin changes to eg. a storage account so you have a better view on the historical changes of all device twins.

    If you really want to delete tags or desire properties, you need to set then to 'null' (check the information on the device twin page in the portal).


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


  2. Sampath 175 Reputation points Microsoft Vendor
    2025-01-30T11:43:28.79+00:00

    Hello @Jeff Marcum
    Greetings!

    Welcome to the Microsoft Q&A Platform. Thank you for reaching out & I hope you are doing well.

    Adding to Sander van de Velde's answer, you can also use Azure CLI like this:

    az iot hub device-twin update --device-id <DeviceID> --hub-name <YourIoTHub> --set tags.location=null

    2025-01-30 16_59_51-Device twin - Microsoft Azure and 27 more pages - Work - Microsoft​ Edge

    Additionally, you can use C# code to update tags and refer this MDOC to use the Azure IoT Hub device SDK and service SDK to develop applications to handle common device twin tasks :

      static RegistryManager registryManager;
        static string connectionString = "<Your IoT Hub Connection String>";
        static async Task Main()
        {
            registryManager = RegistryManager.CreateFromConnectionString(connectionString);
            await RemoveTag("sampath");  // Replace with your actual Device ID
        }
        static async Task RemoveTag(string deviceId)
        {
            var twin = await registryManager.GetTwinAsync(deviceId);
            var patch = @"{
                ""tags"": {
                    ""deploymentLocation"": {
                        ""building"": null
                    }
                }
            }";
            await registryManager.UpdateTwinAsync(deviceId, patch, twin.ETag);
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.