How to delete a file in Swift

October 15, 2017

To delete a file in iOS development with Swift you can use the removeItemAtPath() method in 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 oldFile = playgroundURL.appendingPathComponent("test-new.txt").path


do{
    try fileManager.removeItem(atPath: oldFile)
} catch   {
    print("error")
}