'Delete obsolete reported properties from Azure iot edge module Twin

I am still rather inexperienced to Microsoft Azure iotedge (and stackoverflow - this being my first post) and how module twins work and have an issue regarding the deletion of obsolete properties from the reported part of my module twin.

I have migrated a couple of properties from one device to another within the module twin, however have not been able te remove the properties from the reported and I understand that setting them to null should do the trick (setting them to null and updating them in desired removes them only from the desired part of the twin). The obsolete properties are also not present in the module twin locally on the device

I have tried updating the reported - with a C# console app using the Microsoft.Azure package setting the obsolete properties to null - but this doesnt seem to work either.

await registryManager.UpdateTwinAsync(deviceId, moduleId, removeProperties, eTag);

with my string removeProperties being something like the following (updating desired using this route works like a charm)

{
  {
  "properties": {
    "reported": {
      "foo": {
        "bar": null
      }
    }
  }
}

Can Anybody suggest on a way to remove these properties?



Solution 1:[1]

You can't update the reported properties through the registry manager. However, it can be done using the device's identity. In the same way you wrote a console program to update the twin with the service SDK, you could do it with the device SDK (granted that the device is offline).

For instance if you have a file called twin.json:

{
  "foo": {
     "bar": null
  }
}

You can update the reported properties like this:

var text = await File.ReadAllTextAsync("./twin.json");
var deviceClient = DeviceClient.CreateFromConnectionString("very-secret-connection-string");
var twinCollection = new TwinCollection(text);
await deviceClient.UpdateReportedPropertiesAsync(twinCollection);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matthijs van der Veer