So what is this tree stuff? =========================== In order to get correct folder deletion across the board for all different RFC2060 compliant IMAP servers, deleting of folders needed to work by deleting subfolders (inferiors) first, working up to the top folder that is desired to be deleted. The best way to do this was to use a tree, and walk the thing in preorder to get subfolders first (leaves), working our way up the tree. I chose to use an array for the tree structure. The array has the following elements: $tree[0]["value"] = $tree[0]["doIHaveChildren"] = boolean $tree[0]["subNodes"] = indexes of the array that are child nodes of this node The trickiest part was finding the correct parent node when creating a new node in the tree. Check the documentation in the code for more info on this. Basically all we had to do as loop through each of the items that need to be in the tree (folders, subfolders), find their parent, let their parent know it has a new child, and insert the values into the child. Once the tree is generated, a simple preorder or postorder walk of the tree can be done doing whatever function you desire (delete, create, etc). Preorder walking gives you the tree from the leaves up. Postorder walks the tree from the root node down to the leaves.