deleteRecursively

JVM
JRE7
1.8
@ExperimentalPathApi fun Path.deleteRecursively()
(source)

Recursively deletes this directory and its content. Note that if this function throws, partial deletion may have taken place.

If the entry located by this path is a directory, this function recursively deletes its content and the directory itself. Otherwise, this function deletes only the entry. Symbolic links are not followed to their targets. This function does nothing if the entry located by this path does not exist.

If the underlying platform supports SecureDirectoryStream, traversal of the file tree and removal of entries are performed using it. Otherwise, directories in the file tree are opened with the less secure Files.newDirectoryStream. Note that on a platform that supports symbolic links and does not support SecureDirectoryStream, it is possible for a recursive delete to delete files and directories that are outside the directory being deleted. This can happen if, after checking that an entry is a directory (and not a symbolic link), that directory is replaced by a symbolic link to an outside directory before the call that opens the directory to read its entries.

If an exception occurs attempting to read, open or delete any entry under the given file tree, this method skips that entry and continues. Such exceptions are collected and, after attempting to delete all entries, an IOException is thrown containing those exceptions as suppressed exceptions. Maximum of 64 exceptions are collected. After reaching that amount, thrown exceptions are ignored and not collected.

Exceptions

IOException - if any entry in the file tree can't be deleted for any reason.