tree data UPDATE updating NP cont dflt flag
diff --git a/src/parser_common.c b/src/parser_common.c
index cd32300..cf509ca 100644
--- a/src/parser_common.c
+++ b/src/parser_common.c
@@ -266,10 +266,13 @@
             /* delete the metadata */
             if (prev_meta) {
                 prev_meta->next = meta2->next;
-            } else {
+            } else if (meta != &node->meta) {
                 *meta = (*meta)->next;
             }
             lyd_free_meta_single(meta2);
+
+            /* update dflt flag for all parent NP containers */
+            lyd_cont_set_dflt(lyd_parent(node));
             break;
         }
 
diff --git a/src/tree_data.c b/src/tree_data.c
index e3d7ac9..4b24714 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -936,17 +936,7 @@
         }
 
         /* check for NP container whether its last non-default node is not being unlinked */
-        if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
-                !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
-            LY_LIST_FOR(node->parent->child, iter) {
-                if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
-                    break;
-                }
-            }
-            if (!iter) {
-                node->parent->flags |= LYD_DEFAULT;
-            }
-        }
+        lyd_cont_set_dflt(lyd_parent(node));
 
         node->parent = NULL;
     }
diff --git a/src/tree_data_common.c b/src/tree_data_common.c
index 31dd2d2..67a9586 100644
--- a/src/tree_data_common.c
+++ b/src/tree_data_common.c
@@ -1010,6 +1010,35 @@
     return schema;
 }
 
+void
+lyd_cont_set_dflt(struct lyd_node *node)
+{
+    const struct lyd_node *child;
+
+    while (node) {
+        if (!node->schema || (node->flags & LYD_DEFAULT) || !lysc_is_np_cont(node->schema)) {
+            /* not a non-dflt NP container */
+            break;
+        }
+
+        LY_LIST_FOR(lyd_child(node), child) {
+            if (!(child->flags & LYD_DEFAULT)) {
+                break;
+            }
+        }
+        if (child) {
+            /* explicit child, no dflt change */
+            break;
+        }
+
+        /* set the dflt flag */
+        node->flags |= LYD_DEFAULT;
+
+        /* check all parent containers */
+        node = lyd_parent(node);
+    }
+}
+
 /**
  * @brief Comparison callback to match schema node with a schema of a data node.
  *
diff --git a/src/tree_data_internal.h b/src/tree_data_internal.h
index 3d6b510..fd0792d 100644
--- a/src/tree_data_internal.h
+++ b/src/tree_data_internal.h
@@ -126,6 +126,13 @@
 const struct lysc_node *lyd_node_schema(const struct lyd_node *node);
 
 /**
+ * @brief Set dflt flag for a NP container if applicable, recursively for parents.
+ *
+ * @param[in] node Node whose criteria for the dflt flag has changed.
+ */
+void lyd_cont_set_dflt(struct lyd_node *node);
+
+/**
  * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
  * Uses hashes - should be used whenever possible for best performance.
  *
diff --git a/src/validation.c b/src/validation.c
index 31359a0..d64f989 100644
--- a/src/validation.c
+++ b/src/validation.c
@@ -1520,16 +1520,7 @@
         LY_CHECK_RET(lyd_validate_final_r(lyd_child(node), node, node->schema, NULL, val_opts, int_opts, must_xp_opts));
 
         /* set default for containers */
-        if (node->schema && (node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
-            LY_LIST_FOR(lyd_child(node), next) {
-                if (!(next->flags & LYD_DEFAULT)) {
-                    break;
-                }
-            }
-            if (!next) {
-                node->flags |= LYD_DEFAULT;
-            }
-        }
+        lyd_cont_set_dflt(node);
     }
 
     return LY_SUCCESS;