Delete a directory in Kotlin
This post will discuss how to delete a directory and all entries in it with Kotlin.
1. Using File.deleteRecursively() function
A simple solution to delete a directory with all its subdirectories is using the File.deleteRecursively() function. It returns true if the directory is successfully deleted; false otherwise. The following code demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.io.File fun main() { val file = File("/var/www/html/") try { file.deleteRecursively() println("Directory deleted successfully.") } catch (e: Exception) { e.printStackTrace() } } |
2. Using Files.walkFileTree() function
The idea is to walk the file tree using Files.walkFileTree() function, and delete each file using the Files.delete() function. If the file is a subdirectory, delete it after visiting it and deleting all files in it. The following example demonstrates the usage of these functions to delete a directory:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.io.File import java.io.IOException import java.nio.file.FileVisitResult import java.nio.file.Files import java.nio.file.Path import java.nio.file.SimpleFileVisitor import java.nio.file.attribute.BasicFileAttributes fun deleteDirectory(directory: Path) { if (Files.exists(directory)) { Files.walkFileTree(directory, object : SimpleFileVisitor<Path>() { override fun visitFile(path: Path, attr: BasicFileAttributes): FileVisitResult { Files.delete(path) return FileVisitResult.CONTINUE } override fun postVisitDirectory(path: Path, ex: IOException): FileVisitResult { Files.delete(path) return FileVisitResult.CONTINUE } }) } } fun main() { val file = File("/var/www/html/") try { deleteDirectory(file.toPath()) println("Directory deleted successfully.") } catch (e: IOException) { e.printStackTrace() } } |
3. Using Files.walk() function
Alternately, we can walk the file tree in a depth-first manner using the Files.walk(…) function that returns a Stream of Path objects. The following code demonstrates the working of the Files.walk(…) function with the File.delete() function to recursively delete the root directory and all its subdirectories.
Note that Comparator.reverseOrder() is used to ensure the parent directory is deleted after the child (Since the directory is processed depth-first and File.delete() cannot delete a non-empty directory).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.File import java.io.IOException import java.nio.file.Files import java.nio.file.Path import java.util.* fun deleteDirectory(directory: Path?) { Files.walk(directory) .sorted(Comparator.reverseOrder()) .map { it.toFile() } .forEach { it.delete() } } fun main() { val file = File("/var/www/html/") try { deleteDirectory(file.toPath()) println("Directory deleted successfully.") } catch (e: IOException) { e.printStackTrace() } } |
4. Using Runtime.exec() function
Another plausible solution is to execute the remove directory command of the underlying environment. This can be done in Kotlin using the Runtime.exec() convenience function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.io.File import java.io.IOException fun deleteDirectory(file: File) { println(file.absolutePath) if (file.exists()) { val cmd = "rm -r " + file.absolutePath Runtime.getRuntime().exec(cmd) } } fun main() { val file = File("/var/www/html/") try { deleteDirectory(file) println("Directory deleted successfully.") } catch (e: IOException) { e.printStackTrace() } } |
5. Using File.delete() function
Finally, we can write a recursive routine to delete a directory with the File.delete() function. This would translate to a simple code below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.File fun deleteDirectory(file: File) { if (file.isDirectory) { val contents = file.listFiles() for (f in contents) { deleteDirectory(f) } } file.delete() } fun main() { val file = File("/var/www/html/") deleteDirectory(file) println("Directory deleted successfully.") } |
That’s all about deleting a directory in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)