Remove the file from document directory iOS swift

 Solution:

// MARK: - remove the document directory file

func removeFileDoc(itemName: String) {

    let fileManager = FileManager.default

    let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory

    let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask

    let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)

    guard let dirPath = paths.first else {

        return

    }

    if itemName != "" {

        let filePath = "\(dirPath)/\(itemName)"

        do {

            try fileManager.removeItem(atPath: filePath)

        } catch let error as NSError {

            print(val:error.debugDescription)

        }

    }

}


You can send the filename to the above function.It will delete the file that was stored in document directory will be deleted.

Comments