in reply to Twig delete not deleting the entire section?
I think that you're being caught by the fact that Twig is, by design, an incremental parser. It never builds an in-memory data structure as LibXML2 does. But maybe in this case this is what you need to be doing. Twig will call a handler as soon as it recognizes the need for it and before it has processed anything else in the input. It is therefore good for processing arbitrarily large XML files without a correspondingly-high memory burden, but it is not well-suited to structure manipulation or modification for the reasons stated. If you need to modify, use LibXML2 which will parse the entire thing into a data structure that you can then manipulate and rewrite.