Sometimes diagram.model.removeLinkData(linkdata) not working

Hi,

I been having this problem since last night, I want to delete some links from the diaram programmtically. Here’s the code.

let linksToRemove = [];
node.findLinksInto().each(function(link) {
	linksToRemove.push(link);
});
node.findLinksOutOf().each(function(link) {
	linksToRemove.push(link);
});

linksToRemove.forEach(function(link){
	console.log(diagram.model.containsLinkData(link.data)); // return false  !!!! This should be true. some times return true though 
	diagram.model.removeLinkData(link);
	console.log(diagram.model.containsLinkData(link.data)); // return false
});

This result in sometime the links are deleted while sometime is not. I dont know what could be the issue?

Cheers,

I suggest using a go.Set and Diagram.removeParts instead.

diagram.startTransaction('removing links');
diagram.removeParts(new go.Set().addAll(node.linksConnected));
diagram.commitTransaction('removing links');

The problem with your original code, @Kakit, is that you were calling GraphLinksModel.removeLinkData on the Link instead of on the Link.data. Only the data is in the model, not the Link (or Node) object.

In addition, Simon has improved the code to be clearer and simpler.

Hi walter,

I noticed that, it was my typo when I posted on this forum, in my code it is link.data

Cheers,