1. Introduction

Deleting files and subdirectories in a directory is a common requirement in many applications, such as file management tools, cleanup scripts, and more. Kotlin offers efficient and concise ways to handle file operations.

In this tutorial, we’ll explore how to delete files and subdirectories in a directory using Kotlin.

2. Understanding File Deletion in Kotlin

Kotlin provides direct access to Java’s file I/O APIs, making it straightforward to perform file operations. We’ll primarily use the java.io.File class to achieve our objective. It’s crucial to understand that deleting a directory in Kotlin requires the directory to be empty. Therefore, we need to recursively delete all subdirectories and files first.

3. Deleting a Single File

Before we dive into deleting directories, let’s start with the basics of deleting a single file:

fun deleteFile(filePath: String) {
    val file = File(filePath)
    if (file.exists() && file.isFile) {
        file.delete()
    }
}

This function checks if the file exists and is indeed a file (not a directory) before attempting to delete it. Let’s write a simple unit test to validate the function we just wrote:

@Test
fun `given file path when deleteFile called then file is deleted`() {
    val tempFile = createTempFile()
    assertTrue(tempFile.exists())

    deleteFile(tempFile.absolutePath)

    assertFalse(tempFile.exists())
}

4. Deleting Files and Subdirectories in a Directory

To delete a directory along with its contents, we need to recursively traverse the directory tree:

fun deleteDirectory(directory: File) {
    if (directory.exists() && directory.isDirectory) {
        directory.listFiles()?.forEach { file ->
            if (file.isDirectory) {
                deleteDirectory(file)
            } else {
                file.delete()
            }
        }
        directory.delete()
    }
}

In this function, listFiles() is used to get an array of File objects representing the contents of the directory. We then recursively call deleteDirectory() for each subdirectory and use delete() for files. Lastly, we can delete each directory. Let’s also create a unit test to validate our function:

@Test
fun `given directory when deleteDirectory called then directory and its contents are deleted`() {
    val tempDir = createTempDir()
    val tempFileInDir = File(tempDir, "tempFile.txt").apply { createNewFile() }
    assertTrue(tempDir.exists())
    assertTrue(tempFileInDir.exists())

    deleteDirectory(tempDir)

    assertFalse(tempDir.exists())
    assertFalse(tempFileInDir.exists())
}

5. Handling Exceptions

File deletion can fail for various reasons, such as insufficient permissions or file locks. It’s good practice to handle potential exceptions:

fun safeDeleteDirectory(directory: File) {
    try {
        deleteDirectory(directory)
    } catch (e: IOException) {
        e.printStackTrace() // Or handle the exception as needed
    }
}

Let’s also write a unit test for our function:

fun `given a non-existent file should not throw`() {
    val file = File("imaginary-file.txt")

    assertDoesNotThrow {
        safeDeleteDirectory(file)
    }
}

In this test, we validate that safeDeleteDirectory() won’t throw any exception for an invalid file

6. Using Extensions for Cleaner Code

Kotlin’s extension functions provide a powerful way to enhance existing classes with new functionality. In our case, we can add an extension function to the java.io.File class to make our code more idiomatic and concise.

Specifically, it’s worth noting that Kotlin’s standard library already provides a deleteRecursively() function for File. To avoid confusion, we’ll create a custom extension function with a different name that leverages Kotlin’s walkBottomUp() function for directory traversal.

6.1. Kotlin’s Built-in deleteRecursively() Function

Kotlin’s standard library includes a convenient method, deleteRecursively(), for deleting a directory and all its contents. This function offers an all-in-one solution for simple use cases where recursive deletion is required without additional processing:

val success = File("/path/to/directory").deleteRecursively()

While deleteRecursively() is effective for straightforward scenarios, it may not cover all use cases. For example, if we need to perform additional operations before deleting each file or directory, or if we require more detailed error handling, this built-in method might not suffice.

6.2. Creating a Custom Extension Function

Finally, to address the limitations of the built-in deleteRecursively() function, we can create a custom extension function, deleteContentsRecursively(). This approach allows us to add specific logic or handle unique cases:

fun File.deleteContentsRecursively(): Boolean {
    if (!this.exists()) return false
    if (!this.isDirectory) return this.delete()
    return this.walkBottomUp().all { it.delete() }
}

In this function, walkBottomUp() is used to traverse all files in the directory and its subdirectories, starting from the deepest level. We can then delete each file and directory encountered during the traversal, and also insert custom logic as needed. Let’s validate our function with a unit test:

@Test
fun `given directory when deleteDirectory called then directory and its contents are deleted recursively`() {
    val tempDir = createTempDir()
    val innerTempDir = File(tempDir, "innerTempDir").apply { mkdir() }
    val tempFileInDir = File(innerTempDir, "tempFile.txt").apply { createNewFile() }

    assertTrue(tempDir.exists())
    assertTrue(innerTempDir.exists())
    assertTrue(tempFileInDir.exists())

    tempDir.deleteContentsRecursively()

    assertFalse(tempDir.exists())
    assertFalse(innerTempDir.exists())
    assertFalse(tempFileInDir.exists())
}

7. Conclusion

In this article, we discussed how to delete files and subdirectories in a directory in Kotlin. We covered basic file deletion, recursive directory deletion, exception handling, and the use of extension functions for more idiomatic Kotlin code. This knowledge is essential for tasks that require file system manipulation in Kotlin-based applications.

Remember, file deletion is a permanent operation. Always ensure that you have the correct paths and necessary backups before performing delete operations in a production environment.

As always, the code used in this article is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.