How to delete a directory with multiple btrfs subvolumes

btrfsrm

My root file system is on a btrfs partition. My /chroot directory has a number of btrfs subvolumes in it which prevent me from deleting it with rm -rf /chroot. In an attempt to delete all the subvolumes, I tried listing just the subvolumes below /chroot by using the -o, which the man page says "print only subvolumes below specified path".

# btrfs subvolume list -o /chroot
ID 593 gen 6616 top level 5 path chroot/base-devel/root
ID 594 gen 6618 top level 5 path chroot/multilib-devel/root
ID 595 gen 6620 top level 5 path chroot/base/root
ID 597 gen 6624 top level 5 path chroot/twm/root
ID 599 gen 6628 top level 5 path chroot/lxde/root
ID 601 gen 6655 top level 5 path chroot/wheezy/root
ID 602 gen 6684 top level 5 path chroot/sid/root
ID 603 gen 6862 top level 5 path var/lib/machines

To my surprise, this returns the subvolume in /var/lib/macines (which I do not want to delete). This leads me to 3 questions:

  1. What does the -o option do?

  2. How do I list only subvolumes below /chroot

  3. How can I delete a directory with an unknown number of subvolumes?

I am running Arch Linux with the 4.2.5 kernel and btrfs-progs v4.3.1

Best Answer

The following command removes all subvolumes in the /chrootdirectory:

ls /chroot | xargs btrfs subvolume delete

In case there are files or directories that are not btrfs subvolumes present, an error will be printed, but they are not removed.

(Tested on Debian with the 4.6.0 kernel and btrfs-progs v3.17)