NSDocumentDirectory remove folder

Viewed 8613

I have created a folder inside documents directory using :

fileManager.createDirectory(atPath:ziPFolderPath,withIntermediateDirectories: false, attributes: nil)  

In this folder I have placed few files.
Later in the app, I want to delete not just the files inside the above folder, but also the folder.
FileManager supports removeItem function but I am wondering if it removes the folder as well.

3 Answers

Swift 5

Also you should check if file exist at path or not and check for error also.

do {
    let fileManager = FileManager.default

    // Check if file exists
    if fileManager.fileExists(atPath: urlfilePath) {
        // Delete file
        try fileManager.removeItem(atPath: urlfilePath)
    } else {
        print("File does not exist")
    }
} catch {
    print("An error took place: \(error)")
}
Related