Dynamically remove shape from canvas


I am creating shapes to diagram via

 var canvas = this.get("canvas"),
    elementFactory = this.get("elementFactory");

  var customAttrs = assign({ businessObject: customElement }, customElement);

  var customShape = elementFactory.create("shape", customAttrs);

  return canvas.addShape(customShape);

How can I now safely delete the customShape from canvas if I know the id of the element?

Modeling.removeElements is your friend here.

var elementRegistry = modeler.get('elementRegistry'),
  modeling = modeler.get('modeling');

var element = elementRegistry.get(id);

modeling.removeElements([ element ]);
1 Like

error when i use the code above;

it’s seems like the right code is this

modeling = modeler.get('modeling');

You’re right, I fixed @Niklas_Kiefer code block. :+1:

1 Like