'swift delete image file, throw file does not exist

 let filename = getDocumentsDirectory().appendingPathComponent(upload.fileName)
               print("deleting")
            let fileNameToDelete = upload.fileName
            var filePath = ""          
            // Fine documents directory on device
            let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)          
            if dirs.count > 0 {
                let dir = dirs[0] //documents directory
                filePath = dir.appendingFormat("/" + fileNameToDelete)
                print("Local path = \(filePath)")      
            } else {
                print("Could not find local directory to store file")
                return
            }         
            print(filename)           
              print("deleting111111")
            do {
                let fileManager = FileManager.default  
                // Check if file exists
                print("filePath")
                print(filePath)
                 print("filePath")
                if fileManager.fileExists(atPath: filePath) {
                    // Delete file
                    try fileManager.removeItem(atPath: filePath)
                } else {
                    print("File does not exist")
                }            
            }
            catch let error as NSError {
                print("An error took place: \(error)")
            }}

This gets printed below. Why is delete not working? Why is above function throwing to me on file exists it does not exist

deleting
Local path = /var/mobile/Containers/Data/Application/C763B3ED-3371-47AB-8F61-4F086D01E430/Documents/profile-FFCEBEA9-2F8D-49E2-9A09-2BF87BD0B542--A9636AF4-350D-4D72-A4BD-E4F2B183F4BB.png
file:///var/mobile/Containers/Data/Application/C763B3ED-3371-47AB-8F61-4F086D01E430/Documents/profile-FFCEBEA9-2F8D-49E2-9A09-2BF87BD0B542--A9636AF4-350D-4D72-A4BD-E4F2B183F4BB.png
deleting111111
filePath
/var/mobile/Containers/Data/Application/C763B3ED-3371-47AB-8F61-4F086D01E430/Documents/profile-FFCEBEA9-2F8D-49E2-9A09-2BF87BD0B542--A9636AF4-350D-4D72-A4BD-E4F2B183F4BB.png
filePath
File does not exist


Solution 1:[1]

If you created the file by writing to a filename generated with

filename = getDocumentsDirectory().appendingPathComponent(nameOfImage+"??.PNG")

...then it is absolutely crucial that, when the time comes to delete the file, you generate the filename in exactly the same way. That is not what you are doing.

Indeed, in the code you have shown, you do generate a variable called filename with code that looks similar:

let filename = getDocumentsDirectory().appendingPathComponent(upload.fileName)

...but then you never use filename for anything! Thus you keep faking yourself out. You create filename, you print filename, but you do not use filename as the path to delete. You use some other variable, filePath, obtained in a different way.

Solution 2:[2]

put your remove code in DispatchQueue.main.async like this:

let filename = getDocumentsDirectory().appendingPathComponent(upload.fileName)
           print("deleting")
        let fileNameToDelete = upload.fileName
        var filePath = ""          
        // Fine documents directory on device
        let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)          
        if dirs.count > 0 {
            let dir = dirs[0] //documents directory
            filePath = dir.appendingFormat("/" + fileNameToDelete)
            print("Local path = \(filePath)")      
        } else {
            print("Could not find local directory to store file")
            return
        }         
        print(filename)           
          print("deleting111111")
        do {
            let fileManager = FileManager.default  
            // Check if file exists
            print("filePath")
            print(filePath)
             print("filePath")
            if fileManager.fileExists(atPath: filePath) {
                // Delete file

                DispatchQueue.main.async { // <- here
                  try fileManager.removeItem(atPath: filePath)
                }
                
            } else {
                print("File does not exist")
            }            
        }
        catch let error as NSError {
            print("An error took place: \(error)")
        }}

Solution 3:[3]

Found an answer on another site. Edit the info tab in the scheme to identify the binary.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 matt
Solution 2 Amin Rezaew
Solution 3 codegen