Delete All Files in Documents Directory

In a recent project, I was using iTunes File Sharing to store various log files and test data. Although you can quickly delete all the files in the Documents directory using iTunes, I also was looking for a way to clean up the same directory when my device was not connected.

The first approach I took was to loop through all the files, building a path to each, one by one:

// Path to the Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0)
{
  NSError *error = nil;  
  NSFileManager *fileManager = [NSFileManager defaultManager];
 
  // Print out the path to verify we are in the right place
  NSString *directory = [paths objectAtIndex:0];
  NSLog(@"Directory: %@", directory);
 
  // For each file in the directory, create full path and delete the file
  for (NSString *file in [fileManager contentsOfDirectoryAtPath:directory error:&error])
  {    
    NSString *filePath = [directory stringByAppendingPathComponent:file];
    NSLog(@"File : %@", filePath);
 
    BOOL fileDeleted = [fileManager removeItemAtPath:filePath error:&error];
 
    if (fileDeleted != YES || error != nil)
    {
      // Deal with the error...
    }
  }
 
}

The code above works fine, however, I was interested in something that didn’t build each path separately – the code below deletes all the files in one fell swoop:

 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 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:&error];
 
  if (deleted != YES || error != nil)
  {
    // Deal with the error...
  }
 
}

Given the requested item to remove was a directory, as you’d expect, not only were the files deleted, so was the Documents directory. Problem is, I ran into errors when I attempted to drag/drop new files into the file sharing areas iTunes.

To get things working again, I changed up the code above to create the Documents directory:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0)
{
  NSLog(@"Path: %@", [paths objectAtIndex:0]);
 
  NSError *error = nil;  
  NSFileManager *fileManager = [NSFileManager defaultManager];
 
  // Remove all files in the documents directory
  BOOL deleted = [fileManager removeItemAtPath:[paths objectAtIndex:0] error:&error];
 
  if (deleted != YES || error != nil)
  {
    // Deal with the error...
  }
  else
    // Recreate the Documents directory
    [fileManager createDirectoryAtPath:[paths objectAtIndex:0] withIntermediateDirectories:NO attributes:nil error:&error];
 
}

Although there is less code to delete the Documents directory, something tells me it may simply be a better (and more robust) solution to loop and delete each file in sequence.