Removing a part

Hi,
My issue is I want to remove a part which is actually a background.

Suppose this is how I added a part

myDiagram.add(
scope.gj(go.Part, // this Part is not bound to any model data
{
layerName: “Background”, position: new go.Point(0, 0),
selectable: false, pickable: false
},
scope.gj(go.Picture, {
source: img.src,
//width: scope.totalDiagramWidth,
//height: scope.totalDiagramHeight
},
new go.Binding(“width”, “widthOfCanvas”),
new go.Binding(“height”, “heightOfCanvas”)
)
))

Now I want to remove it
How is it possible please?

If that was the only Part that you added, it should be the value of myDiagram.parts.first()

By the way, scope.gj is very suspicious. You really should be using go.GraphObject.make.

@walter Make total sense to me
But I am adding a bunch of parts on the flow
How can I remove a specific part?
Is there any way I can search a part by name or something like this and then remove it

Of course you can keep references to the Parts that you add in JavaScript.

Or I suppose you can give them GraphObject.names as you add them. Later you can search in the Diagram.parts collection for the Part with the name that you care about.

@walter
Exactly that is what I am digging into. I am new with gojs so struggling a little
I am looking for a best iteration function that can return me a part with a particular name.
Suppose the name of the part is “test” that I want to remove
How I can search for a part with name “test”?
I know gojs have some functions like findPartForData and findPartForKey but none of them are working .

function findPart(name) {
  var it = myDiagram.parts;
  while (it.next()) {
    if (it.value.name === name) return it.value;
  }
  return null;
}

it worked thanks Walter