Troubleshooting
Why MQTT Engine keeps tags after a JSON array shrinks
Short answer
MQTT Engine only creates and refreshes tags for fields present in an arriving payload, so when a JSON array shrinks, the removed index's branch keeps its last value; publishing null does not delete object-derived tags. Remove the obsolete indexed branch with a Gateway Timer Script that compares the payload's indexes against the existing branches, or delete it once in the MQTT Engine provider.

On this page
MQTT Engine has no built-in cleanup for a JSON array that shrinks. The module creates and refreshes tags for the fields present in an arriving payload; a missing element carries no delete instruction, so the branch for the removed index keeps its last value. The fix is to delete the obsolete indexed branch yourself, typically with a Gateway Timer Script that compares the payload's current indexes against the branches under the array path and removes the extras. Publishing null entries does not delete object-derived tags, and a module restart is a one-off tool, not a policy. What follows is where the data path fails, which trigger to choose, and a worked cleanup of the reported case.
Where the shorter array stops affecting the tags #
The publisher first sends three objects under operations[0].bom; MQTT Engine parses them and creates indexed branches 0, 1 and 2 under [MQTT Engine]devices/device1/operations/0/bom. A later message carries only indexes 0 and 1. Those two refresh normally, but the absence of index 2 is never read as a delete instruction, so the tag at [MQTT Engine]devices/device1/operations/0/bom/2/materialId holds its previous value. The fault sits in tag-tree reconciliation, not in transport or decoding, and one pass along the data path proves it:
- Publisher and topic. The current document contains two bom objects with materialId 1 and 2. If three objects are still on the wire, correct the publisher before touching any tags.
- Broker, subscription and parser. Because indexes 0 and 1 take the new values, the transport path, the subscribed topic and the JSON decoding are all proven working. A decode fault would corrupt the fresh values, not preserve only the obsolete branch.
- Tag reconciliation. Branches 0 and 1 refresh while branch 2 keeps its old value. This is the failure point and the only place a fix applies.
If every tag on the gateway shows old values rather than one branch, you are looking at a broker interruption instead — see why MQTT Engine still feeds old tags after a broker stops.
Why the third branch survives #
MQTT transports application payloads; it does not define how a subscriber reconciles an arbitrary JSON document with an existing tag hierarchy. MQTT Engine materializes a tag for every field that arrives, and a field that stops arriving supplies no value, no type and no delete operation. Removing an element at the publisher therefore changes the current document without issuing anything against the previously created branch. The array is the publisher's current state; the tag tree is state accumulated from every earlier message. Without lifecycle logic the two models diverge, one stale branch at a time.
Sending "bom": [null, null] does not close the gap. The elements are expected to be objects, and a null entry does not identify which existing object-derived branches to destroy, so the previously created tags stay untouched. Restarting MQTT Engine does clear the generated structure, but the tree rebuilds only from messages that arrive again — acceptable once after a deliberate payload redesign, wrong as a routine answer.
Choosing the cleanup trigger #
Commit to a Gateway Timer Script unless every payload carries a proven complete-message indicator. The timer keeps structural maintenance independent of the order in which child tags update and concentrates the deletion logic in one gateway-scoped process.
| Approach | Effect on the stale branch | When it runs | Trade-off |
|---|---|---|---|
| Publish the shorter array only | Refreshes indexes 0 and 1, leaves 2 | Every message | No deletion semantics at all |
| Publish null elements | Object-derived tags remain | On receipt | A null value is not a structural delete |
| Restart MQTT Engine | Whole generated tree cleared for rebuild | During restart | Every quiet device loses its tags until it republishes |
| Gateway Timer Script | Obsolete branches deleted after a compare | At the timer's configured delay | A stale branch lives until the next run |
| Tag Change Script | Same compare-and-delete | Event-driven | Safe only when the trigger proves the full payload arrived |
A Tag Change Script is the right tool only when a single trigger is written after the whole document — a separately published element count, for instance. A change on an arbitrary child fires before its siblings have updated, and the cleanup then races the payload it is meant to describe.
Worked example: reconciling bom from three elements to two on device1 #
Inputs. From the reported case: the first payload carries three objects under operations[0].bom with materialId 1, 2 and 3, so MQTT Engine creates branches 0, 1 and 2 under [MQTT Engine]devices/device1/operations/0/bom. The later payload carries two objects, materialId 1 and 2. Read from the tag tree: the numeric child branch names {0, 1, 2}. Read from the current payload: the expected positional indexes {0, 1}.
Rule. The obsolete set is the existing numeric branch names minus the expected indexes taken from the current payload, never from the tag tree, which contains the stale branch being diagnosed. Delete every branch in the obsolete set as a whole object branch; an empty set means the run deletes nothing.
Arithmetic. {0, 1, 2} − {0, 1} = {2}. One obsolete index.
Result. Delete the object branch [MQTT Engine]devices/device1/operations/0/bom/2, not just its materialId leaf, so no empty folder or sibling tags survive.
Illustrative pseudocode follows; the variable names are ours, not API calls:
parentTagPath = "[MQTT Engine]devices/device1/operations/0/bom" # string, from the reported case
expected = {0, 1} # set of integers, read from the current payload
existing = {0, 1, 2} # set of integers, read from numeric child branch names
obsolete = existing - expected # evaluates to {2}
for each index in obsolete:
delete the branch parentTagPath + "/" + index
Decision. A non-empty obsolete set means delete those branches this run and log the device, array path and deleted indexes; an empty set means write nothing and leave the tree alone. Leave indexes 0 and 1 untouched, and leave any future index 2 to normal MQTT Engine creation when the array grows again.
Cleanup mistakes that recreate the problem #
- Deleting during partial child updates. A valid branch is removed before the full message is reflected in the tags. Trigger from a complete-message indicator or rely on the timer's serialized cycle.
- Trusting the generated tag count. Stale branches inflate it indefinitely. Derive expected membership from the current payload or an authoritative count.
- Deleting only a leaf. The empty object branch or sibling tags remain behind. Delete the obsolete indexed object branch.
- Treating null as delete. The old object-derived tags remain untouched; use an explicit deletion.
- Comparing across messages. Capture membership, compare and delete as one ordered cycle per device and array path, so an old payload snapshot never meets tags produced by a newer message.
- Deleting outside the one array path. Keep the delete scope to the branches the compare proved obsolete; a broad deletion in the MQTT Engine provider takes live tags with it.
- Forgetting the historian. The deletion removes the tag from the provider but not the samples already stored, and the historian tag browser can keep the old path as a stale entry — see remove stale tags from the Ignition historian tag browser.
Verifying the fix end to end #
- Publish the three-object payload and confirm
bom/0,bom/1andbom/2exist with materialId 1, 2 and 3, the same creation path described in writing tags from an MQTT JSON payload. - Send the two-object payload and confirm indexes 0 and 1 refresh with the new message, proving broker path, subscription and parser before any cleanup runs.
- Run or await the cleanup trigger and confirm the complete
bom/2branch is gone while 0 and 1 remain. - Grow the array back to three objects and confirm MQTT Engine recreates index 2 from the new payload instead of restoring the old value.
- Check the cleanup log entry for the deleted index, so the next absence is explainable as your script or as a publisher-side contraction.
Questions asked
Can I delete the obsolete tags by hand instead of scripting?
Yes. For a one-time structural change, delete the obsolete indexed branch directly in the MQTT Engine provider like any other tag. The deletion holds until a later payload carries that index again, at which point MQTT Engine recreates the branch from the new data, so hand cleanup suits a payload redesign, not an array whose length keeps changing.
Does deleting the branch erase the history already stored for it?
No. Deletion removes the tag from the MQTT Engine provider, not the samples already written to the historian. That history stays queryable under the old tag path, and the historian tag browser may keep listing the deleted path as a stale entry, which is a separate cleanup from the tag deletion itself.
Does the same behavior apply to object fields that disappear from the payload?
Yes. The rule is not array-specific: any metric or field absent from a later payload neither refreshes nor deletes the tag created for it earlier, so a renamed key leaves the old branch beside the new one. The same compare-and-delete cleanup applies, with field names taking the place of array indexes.
Does deleting tags affect the subscription or the devices still publishing?
No. Cleanup removes generated tags from the MQTT Engine provider only; the subscribed topic filter and the publisher are untouched. The next message that carries a deleted index recreates its branch from current data, which is exactly the behavior the verification steps rely on.


