Delete dependency in list

I try to restrict delete the port untill subport is exist. for that i tried below code,
Its not pointing properly. I’m not sure what i have missed.

container Port {
    leaf portID {
        type leafref {
            path "/data:cardData/data:port/data:portID";
        }			
        description "A unique port ID";
    }

    list subport {
        key "subPortID";
        uses subport:subPort;
    }
}
grouping subPort {
    description
       "subport on the port";                        
    
    leaf subPortID {
       type string;
    }
}

I have introduced below check to restrict delete port dependency,

grouping subPort {
        description
           "subport on the port";                        
        
        leaf subPortID {
           must "../portID" {   <======= new code
                error-message "client cannot delete, since its is dependent with subport"; <======= new code
            } <======= new code
           type string;
        }
    }

You need one extra "../" in the Xpath in the must statement. The first "../" takes you to the grouping-level, the second "../" takes you out of list support {} to the container-level.

I would also add that in general it is not a good idea to refer to items outside your grouping in a must statement. This limits the contexts where the grouping can be used.

@jjohansson thanks for your suggestion,

I have tried with below code change

        leaf subPortID {
			must "../../portID" {
				error-message "client cannot delete, since its is dependent with subport";
			}
           type string;
        }

its not restricting to delete port.

Right, the must-statement prevents deletion of portID, I mis-read your question. Your must-statement refer to portID and I thought that’s what you tried prevent being deleted.

You can’t prevent deletion of the port-container with must-staments inside the container itself - the must-statements will not be evaluated during validation phase because the container doesn’t exist in the new configuration!)

A must-statement outside the port-container, e.g. in its parent should work.