title: IOS: delete a file in a directory
body: <p>In my app I download a pdf file with an ASiHttpRequest and I have these instructions:</p>
  
  <pre><code>NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
      [currentDownload setDownloadDestinationPath:[documentsDirectory stringByAppendingPathComponent:@"file.pdf"]];
  </code></pre>
  
  <p>it work fine at first time, and I can open this file.pdf, but when I download a second time this pdf, it seems that it not replace the file but do a merge.</p>
  
  <p>before I do this, but it doesn't work where is the problem, or what's the best way to delete this file.pdf from its path?</p>
  
  <pre><code>    - (void) removeFile{
    NSString *extension = @"pdf";
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectoryPDF = [paths objectAtIndex:0];
  
      NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectoryPDF error:NULL];
      NSEnumerator *e = [contents objectEnumerator];
      NSString *filename;
      while ((filename = [e nextObject])) {
  
          if ([[filename pathExtension] isEqualToString:extension]) {
  
              [fileManager removeItemAtPath:[documentsDirectoryPDF stringByAppendingPathComponent:filename] error:NULL];
          }
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>now I use this method</p>
  
  <pre><code>- (void) removeFile{
  
  
      NSError *error;
  
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
      NSString *path = [[paths objectAtIndex:0]stringByAppendingString:@"/file.pdf"];
  
      NSFileManager *fileManager = [NSFileManager defaultManager];
  
      NSLog(@"Documents directory before: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&amp;error]);
  
  
          if([fileManager fileExistsAtPath:path] == YES)
          {
              NSLog(@"file exist and I delete it");
  
              NSFileManager *fileManager = [NSFileManager defaultManager];
              [fileManager removeItemAtPath:path error:&amp;error];
  
              NSLog(@"error:%@", error);
          }
  
      NSLog(@"Documents directory after: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&amp;error]);
  
  
  }
  </code></pre>
  
  <p>this method recognize that in directory there is "file.pdf" in NSLog </p>
  
  <pre><code>NSLog(@"Documents directory before: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&amp;error]);
  </code></pre>
  
  <p>but it crash after </p>
  
  <pre><code>"NSLog(@"file exist and I delete it");" 
  </code></pre>
  
  <p>and I have only a "lldb" in consolle. </p>