3This tool looks for empty directories and deletes them.
9More specifically, it deletes directories which are completely empty, or which only contain files/folders which I don't think are worth keeping (e.g.
`.DS_Store` or
`__pycache__`).
17Deleting empty directories is a common problem, and there's a suggestion on [
Unix Stack Exchange question](
https://unix.stackexchange.com/a/107556/431830):
19> Combining GNU find options and predicates, this command should do the job:
22> find . -type d -empty -delete
25The reason this isn't suitable is because it only deletes directories which are
*completely* empty.
26But sometimes a directory can be non-empty, even if it appears empty.
28For example, this directory on macOS:
30<img src="totally_empty.png" alt="A Finder window for a folder 'totally_empty' which apparently contains no files.">
32I consider this directory to be empty, but it will be skipped by
`find . -type d -empty -delete` because of the hidden [
`.DS_Store` file](https://en.wikipedia.org/wiki/.DS_Store).
34This tool will delete directories which are empty or almost empty -- that is, when they only contain files or folders which I don't think are worth keeping, like
`.DS_Store` or
`__pycache__`.
42You can download compiled binaries from the [
GitHub releases](
https://github.com/alexwlchan/emptydir/releases).
44Alternatively, you can install from source.
45You need Rust installed; I recommend using [Rustup].
46Then clone this repository and compile the code:
49$ git clone
"https://github.com/alexwlchan/emptydir.git"
51$ cargo install --path .
54[
Rustup]:
https://rustup.rs/
62Pass the path of the top-level directory you want to clean as a command-line argument, for example:
68It will print the path to every empty directory that it deletes:
71$ mkdir -p ~/Desktop/foo/bar/baz
73/Users/alexwlchan/Desktop/foo/bar/baz
74/Users/alexwlchan/Desktop/foo/bar
75/Users/alexwlchan/Desktop/foo
79If you pass no arguments, it will look for empty directories in the current directory:
89## Which files/folders are safe to delete?
91Currently the list of files/folders which I consider safe to delete is hard-coded in
`can_be_deleted.rs`:
93* .DS_Store stores some folder attributes used for showing the folder
94 in the Finder, which I don't need to keep
95* `.ipynb_checkpoints` is a folder used by Jupyter Notebooks, but not
96 important if I've deleted the notebooks
97* `.jekyll-cache` is a cache directory used by Jekyll sites, but
98 can be easily regenerated and will be rebuilt regularly as part
99 of the Jekyll build process
100* `.venv` is the name I use for virtual environments, which I can
101 easily regenerate if necessary
102* `__pycache__` is the bytecode cache in Python projects, which is
103 pointless if the original Python files have been removed
104* `Thumbs.db` is a file that contains thumbnails on Windows systems
106If you want to change that list, you need to modify the source code and compile a new version -- it's not a configurable setting.
108Note that these files will only be deleted if they are the only items in a folder.
109If, for example, a folder contains both
`.DS_Store` and some text files, then nothing will be deleted.
115For a detailed explanation of how this tool works, you can read [
my accompanying article](
https://alexwlchan.net/2024/emptydir/).