title: delete a file using swift in ios
body: <pre><code>func getDocumentsDirectory() -&gt; URL {
      let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
      let documentsDirectory = paths[0]
      return documentsDirectory
  }
  </code></pre>
  
  <p>So simply, I get my filename using that function.And it returns correctly for now, how can I delete this file using Swift 3?
  And How can I read the image back like <code>UIImageJPEGRepresentation</code>?</p>
  
  <pre><code>let image_data = UIImageJPEGRepresentation(self.commons.correctlyOrientedImage(image: self.imageSelected.image!),0.5)
  </code></pre>
  
  <p>I did following and its not working</p>
  
  <blockquote>
  <pre><code>let filename = getDocumentsDirectory().appendingPathComponent("l.png")   let
  </code></pre>
    
    <p>fileManager = FileManager.default
                        var filePath = filename
                        // Check if file exists</p>
  
  <pre><code>                if fileManager.fileExists(atPath: filePath.absoluteString) {
                      // Delete file
                      try fileManager.removeItem(atPath: filePath.absoluteString)
                  } else {
                      print("File does not exist")
                  }
  </code></pre>
  </blockquote>
  
  <p>I save the file like this</p>
  
  <blockquote>
  <pre><code>let filename =    getDocumentsDirectory().appendingPathComponent("COPY111.PNG")
          try? image_data?.write(to: filename)
  </code></pre>
  </blockquote>
  
  <p>But I cannot remove it from answers provided below</p>