delete a directory in swift

You can delete a directory using the FileManager class in Swift. The following code shows how to delete a directory with its contents:

main.swift
let fileManager = FileManager.default
let directoryURL = URL(fileURLWithPath: "/path/to/directory")

do {
    try fileManager.removeItem(at: directoryURL)
} catch {
    print("Error deleting directory: \(error)")
}
215 chars
9 lines

In this code, we first create an instance of the FileManager class. Then, we create a URL object for the directory we want to delete.

Next, we use a do-catch block to call the removeItem(at:) method on the fileManager object, passing in the directoryURL object. This removes the directory and all its contents.

If an error occurs while deleting the directory, the catch block prints an error message.

gistlibby LogSnag