score:6

Accepted answer

FIDDLE with a quick example of adding, removing and the adding again an SVG and a contained circle. Hope this helps.

function update() {
    svg.remove();
    svg = d3.selectAll("body").append("svg");
    svg.append("circle")
        .attr("cx",40)
        .attr("cy",40)
        .attr("r",20)
        .style("fill","blue");
}

score:9

I had a similar problem to this. removing the SVG element did not allow me to fully update the data like I wanted.

Instead, I removed the g elements created inside the SVG with this line:

d3.selectAll("g > *").remove()

in my updateGraph() function

Full explanation and situation shown here: Repainting/Refreshing Graph in D3


Related Query