pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> Result<()>Expand description
Removes a directory at this path, after removing all its contents. Use carefully!
This function does not follow symbolic links and it will simply remove the
symbolic link itself. If path itself is a symbolic link to a directory, it will get
removed. If it is a symbolic link to a file, behavior is platform-specific:
currently, it will get removed on Unix-like systems but not on Windows.
§Platform-specific behavior
These implementation details may change in the future.
- “Unix-like”: By default, this function currently corresponds to
openat,fdopendir,unlinkatandlstaton Unix-family platforms, except where noted otherwise. - “Windows”: This function currently corresponds to
CreateFileW,GetFileInformationByHandleEx,SetFileInformationByHandle, andNtCreateFile.
§Time-of-check to time-of-use (TOCTOU) race conditions
See the module-level TOCTOU explanation.
On most platforms, fs::remove_dir_all protects against symlink TOCTOU races by default.
However, on the following platforms, this protection is not provided and the function should
not be used in security-sensitive contexts:
- Miri: Even when emulating targets where the underlying implementation will protect against TOCTOU races, Miri will not do so.
- ESP-IDF, Horizon, PS Vita, QNX, Redox OS, VxWorks: This function does not protect against TOCTOU races, as the underlying platform does not implement the required platform support to do so.
§Errors
See fs::remove_file and fs::remove_dir.
remove_dir_all will fail if remove_dir or remove_file fail on any constituent
paths, including the root path. Consequently,
- The directory you are deleting must exist, meaning that this function is not idempotent.
remove_dir_allwill fail if thepathis not a directory.
Consider ignoring the error if validating the removal is not required for your use case.
This function may return io::ErrorKind::DirectoryNotEmpty if the directory is concurrently
written into, which typically indicates some contents were removed but not all.
io::ErrorKind::NotFound is only returned if no removal occurs.