find -delete does not delete non-empty directories

find's -delete flag works similar to rmdir when deleting directories. If the directory isn't empty when it's reached it can't be deleted.

You need to empty the directory first. Since you are specifying -type d, find won't do that for you.

You can solve this by doing two passes: first delete everything within dirs named __pycache__, then delete all dirs named __pycache__:

find ~ -path '*/__pycache__/*' -delete
find ~ -type d -name '__pycache__' -empty -delete

Somewhat less tightly controlled, but in a single line:

find ~ -path '*/__pycache__*' -delete

This will delete anything within your home that has __pycache__ as part of its path.


There are a couple potential reasons for this.

1) You told it to delete directories only (-type d), and those directories still have files inside them.

2) Your directories only contain other directories, so the -type d will take care of the contents issue. However you are using OS-X, which is largely based on FreeBSD, and the FreeBSD find by default will process the directory before its contents.
However the -depth option exists to solve this problem by telling find to process the directory after its contents.

find ~ -name __pycache__ -type d -ls -delete -depth

This issue does not exist on linux because the -delete option implicitly enables -depth.

 

FreeBSD man 1 find:

 -depth  Always true; same as the non-portable -d option. Cause find to
   perform a depth-first traversal, i.e., directories are visited in
   post-order and all entries in a directory will be acted on before
   the directory itself. By default, find visits directories in
   pre-order, i.e., before their contents. Note, the default is not
   a breadth-first traversal.

GNU man 1 find:

 -depth Process each directory's contents before the directory itself. The -delete
        action also implies -depth.

Tags:

Find

Osx