Is there a way to clear out all nodes from a jsTree that's faster than walking through all the nodes deleting them one-by-one?
7 Answers
$('#tree').jstree("destroy").empty();
This is what worked for me. First destroy jstree elements and associated events, and then empty the div containing jstree.

- 661
- 11
- 22
-
worked for me while updating jstree with new data. Thanks. – Vikram Sharma Aug 04 '15 at 09:07
The simplest way I have found is to simply call .empty
on the div containing the tree.
$('#tree').empty();
You might choose to use a more specific selector as a parameter for empty()
, but this works fine for me.

- 159,648
- 54
- 349
- 530
-
5This is a good thing to do if you also follow it with $('#tree').empty().jstree('destroy'); – Daniel Jordan Apr 19 '13 at 18:16
-
1@DanielJordan Yeah, is better bacause without destroy you cant build a new jstree... Thanks – Dani Mar 30 '17 at 15:04
See the documentation here: http://www.jstree.com/documentation/core
.delete_node ( node )
Removes a node. Triggers an event.
mixed node
This can be a DOM node, jQuery node or selector pointing to the element you want to remove.
It seems you can just do a selector that will delete all the nodes you want, no loops required.

- 12,610
- 4
- 42
- 63
-
That does seem to work. I'd been using remove(), and it wasn't working. – Jeff Dege Jun 09 '11 at 19:13
-
-
My familiarity is low, but it seems you can clean that up to simply be the selector itself and not the nodes themselves. Something along the line of `myJsTree.delete_node('[treediv] > ul > li');` – Adam Terlson Jun 09 '11 at 19:28
The following call will destroy the current instance of jsTree, remove any bound event listeners and obviously achieve your ultimate goal of removing all nodes. But this method is a bit of a over-kill, it has to be said.
$("#DivElementContainingYourTree").jstree("init");

- 914
- 1
- 8
- 21