Deleting an element of the diagram on create.PostExecute

I am trying to delete an element if it goes in my else statement, but all that the this.modeling.removeElements is doing is deleting the line that connects the actual element.
Anyone have any idea what am I doing wrong?
Any help would be appreciated

eventBus.on('commandStack.connection.create.postExecute', (event: any) => {
        console.log('PostExecute:', event);

        const connection = event.context.connection;
        const sourceElement = connection.source.type;
        const targetElement =;
        const targetShape =;
        if(targetElement === 'bpmn:Task'){
          if (sourceElement === 'bpmn:ExclusiveGateway'){
          } else {

            console.log('delete good :::connection',);
            throw new Error("TargetGateway error")

It seems like you want to disallow creating connections in certain cases. You can use custom rules to achieve that so you don’t have to bother with having to delete a connection that shouldn’t have been created in the first place.