Friday 6 June 2014

Delete All Files in Documents Directory


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

  1. // Path to the Documents directory
  2. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  3. if ([paths count] > 0)
  4. {
  5.   NSError *error = nil;  
  6.   NSFileManager *fileManager = [NSFileManager defaultManager];
  7.  
  8.   // Print out the path to verify we are in the right place
  9.   NSString *directory = [paths objectAtIndex:0];
  10.   NSLog(@"Directory: %@", directory);
  11.  
  12.   // For each file in the directory, create full path and delete the file
  13.   for (NSString *file in [fileManager contentsOfDirectoryAtPath:directory error:&error])
  14.   {    
  15.     NSString *filePath = [directory stringByAppendingPathComponent:file];
  16.     NSLog(@"File : %@", filePath);
  17.  
  18.     BOOL fileDeleted = [fileManager removeItemAtPath:filePath error:&error];
  19.  
  20.     if (fileDeleted != YES || error != nil)
  21.     {
  22.       // Deal with the error...
  23.     }
  24.   }
  25.  
  26. }

 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:

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2. if ([paths count] > 0)
  3. {
  4.   NSLog(@"Path: %@", [paths objectAtIndex:0]);
  5.  
  6.   NSError *error = nil;  
  7.   NSFileManager *fileManager = [NSFileManager defaultManager];
  8.  
  9.   // Remove Documents directory and all the files
  10.   BOOL deleted = [fileManager removeItemAtPath:[paths objectAtIndex:0] error:&error];
  11.  
  12.   if (deleted != YES || error != nil)
  13.   {
  14.     // Deal with the error...
  15.   }
  16.  
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:

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2. if ([paths count] > 0)
  3. {
  4.   NSLog(@"Path: %@", [paths objectAtIndex:0]);
  5.  
  6.   NSError *error = nil;  
  7.   NSFileManager *fileManager = [NSFileManager defaultManager];
  8.  
  9.   // Remove all files in the documents directory
  10.   BOOL deleted = [fileManager removeItemAtPath:[paths objectAtIndex:0] error:&error];
  11.  
  12.   if (deleted != YES || error != nil)
  13.   {
  14.     // Deal with the error...
  15.   }
  16.   else
  17.     // Recreate the Documents directory
  18.     [fileManager createDirectoryAtPath:[paths objectAtIndex:0] withIntermediateDirectories:NO attributes:nil error:&error];
  19.  
  20. }
 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.

No comments:

Post a Comment