How to delete a folder in Swift

Last Updated: October 15, 2017

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

try fileManager.removeItem(atPath : folder)
} catch {