How to delete a folder in Swift

In this code you can how we can delete folder in Swift. You have to specify the path of the directory which you want to delete and call the removeItem() of the FileManager class

let home = FileManager.default.homeDirectoryForCurrentUser
let playgroundPath = “Documents/dev/xcode/DatePicker/c”

// get your playground URL
let playgroundURL = home.appendingPathComponent(playgroundPath)

// First create a FileManager instance
let fileManager = FileManager.default

let folder = playgroundURL.appendingPathComponent(“test-directory-new”).path

do{
try fileManager.removeItem(atPath : folder)
} catch {
print(“error”)
}

Leave a Reply

Your email address will not be published. Required fields are marked *