title: ios - delete single files that are downloaded into the documents directory
body: <p>Firstly  = I apologize because I have already tried to ask this once before <a href="https://stackoverflow.com/questions/14154011/ios-removing-any-file-but-not-all-files-from-the-document-directory">here</a></p>
  
  <p>I am really struggling with this:</p>
  
  <pre><code>// Override to support editing the table view.
  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  {    
      if (editingStyle == UITableViewCellEditingStyleDelete)
      {
          // Delete the row from the data source.
  
  
          [_mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
      }
  
  }
  </code></pre>
  
  <p>By using the code above I know it is possible to delete an entry from an array that is displayed in a UITableView. However I am wanting to delete files form my Documents Directory that are downloaded by the user and no longer required.</p>
  
  <p>Now I in the mean time and after more searching this code:</p>
  
  <pre><code>    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectoryPath = [paths objectAtIndex:0];
  
  NSString *file = [[NSString alloc] init];
  
  for(int i=0; i&lt;[paths count]; i++)
  {
      file = [documentsDirectoryPath stringByAppendingFormat:@"/%@",_fileArray];
      NSLog(@"%@", file);
  }
  
  NSFileManager *fileManager = [NSFileManager defaultManager];
  [fileManager removeItemAtPath:file error:NULL];
  </code></pre>
  
  <p>Allows me to list all the files that are in the array in my console and this code:</p>
  
  <pre><code>- (void)removeFile:(NSString*)fileName {
  
      NSFileManager *fileManager = [NSFileManager defaultManager];
  
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  
      NSString *documentsDirectory = [paths objectAtIndex:0];
  
      NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3", fileName]];
  
      [fileManager removeItemAtPath: fullPath error:NULL];
  
      NSLog(@"image removed");
  
  }
  </code></pre>
  
  <p>together with: <code>[self removeFile: _filename];</code> allows me to delete a specific file.</p>
  
  <p>so I am making head way. <strong>But I am really stuck when it comes to a user being able to swipe and delete a file.</strong> Of Course I do not know what file is going to be in the directory.</p>
  
  <p><strong>Secondly - How do I handle being able to load the tableView once all the files are deleted?</strong>
  If I do that using this code:</p>
  
  <pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  if ([paths count] &gt; 0)
  {
      NSLog(@"Path: %@", [paths objectAtIndex:0]);
  
      NSError *error = nil;
      NSFileManager *fileManager = [NSFileManager defaultManager];
  
      // Remove Documents directory and all the files
      BOOL deleted = [fileManager removeItemAtPath:[paths objectAtIndex:0] error:&amp;error];
  
  
  }
  </code></pre>
  
  <p>I get the an termination error - I think that is because the directory has also been removed.</p>
  
  <p>I know there is a bit of code here, but I really hope someone and guide me through this:-)</p>