Skip to content

Graph manipulation

Remove elements or change topology while respecting terminal removed handles.

#Graph changes and lifecycle

remove() deletes the receiver, cascading from a removed node to descendant elements and incident edges. It returns the closure actually removed. Those handles are terminal references; only cached identity methods such as id() and group() remain readable. cy.add() consumes element definitions, columnar input, wire buffers or an iterable of definitions. There is no restore() method and no element-level clone operation.

move() changes topology without replacing identity: { parent } reparents nodes, while { source, target } changes edge endpoints. The modes are exclusive and parent takes precedence if both are present. data() updates application fields; id, edge endpoints and parent are first-class immutable fields. A position write is in model units. positions(fn) maps a function over nodes and writes the resulting coordinates in bulk.

#eles.remove()

// Remove these elements from the graph; incident edges of removed nodes cascade
eles.remove(): Collection<NodeData, EdgeData, Data>

Remove these elements from the graph; incident edges of removed nodes cascade. Already-removed elements are skipped (no second remove event).

Returns Collection<NodeData, EdgeData, Data>

the elements actually removed — the closure, so it can be larger than the receiver: removing a parent brings its descendants, and removing a node brings its incident edges. The returned refs are dead by construction (removals are terminal), so only their cached id()/group() still read

Example

const removed = cy.getElementById('c').remove();
console.log(removed.removed()); // true
console.log(cy.hasElementWithId('c')); // false

Example graph and handle setup

#eles.move(opts)

// Set a node's parent or an edge's endpoints without changing its id or data
eles.move(opts: { source?: string; target?: string; parent?: string | null; }): this

Set a node's parent or an edge's endpoints without changing its id or data.

Use { parent: id } to reparent nodes, or { parent: null } to make them orphans. Use { source: id, target: id } to rewire edges. If parent is present it takes precedence over endpoint keys. A changed parent emits a moveout event before the change and move afterwards.

An unknown parent is ignored. Cyclic parent assignments warn and are ignored.

Parameters
NameDescription
opts: { source?: string; target?: string; parent?: string | null; }

{ parent } to re-parent nodes (null orphans them), or { source, target } to re-point edges; parent wins if both are given

Fields of opts
NameDescription
source: string?
target: string?
parent: string | null?
Returns this

this collection, for chaining

Example

cy.getElementById('ab').move({ target: 'c' });
console.log(cy.getElementById('ab').target().id()); // 'c'

Example graph and handle setup

All guides · Alphabetical API index · Read continuously