How can I clear all children nodes of a data node, but NOT delete the data node itself in zookeeper?

Viewed 971

I have a znode: /test

And /test has two children nodes: /test/data1, /test/data2

How can I delete /test/data1 and /test/data2, but at the same time, NOT delete the node /test?

1 Answers

You can execute something like the following:

zkCli.sh -server xxx ls /test | \ 
grep  "^\[" | \
grep -o -P  "\w*" | \ 
while read znode ; do zkCli.sh -server xxx delete /test/$znode ; done

This is only using zkCli.sh and bash commands but is not optimal because it connects to the ZooKeeper server multiple times (one for each direct child deletion + one to fetch the children list). A more straightforward approach would be to use a ZooKeeper client library like kazoo or ZooKeeper Java API for such a task.

Related