Parth Bhatt
Parth Bhatt

Reputation: 19469

How to delete all the files under my app's NSDocumentsDirectory?

I tried using

NSString *DocumentsDirectoryPath = //Code to fetch the Documents Directory Path;
NSError *error;
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtPath:DocumentsDirectoryPath error:&error];

The above code deletes the whole Documents directory of my app, but I just want to delete contents of the Documents directory not the Documents directory itself.

What should I do?

Upvotes: 2

Views: 2473

Answers (1)

Rayfleck
Rayfleck

Reputation: 12106

 NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
 NSFileManager *localFileManager=[[NSFileManager alloc] init];
 NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir];

 NSString *file;
 NSError *error;
 while ((file = [dirEnum nextObject])) 
 {
     NSString *fullPath = [NSString stringWithFormat:@"%@/%@", docsDir,file];
     // process the document
     [localFileManager removeItemAtPath: fullPath error:&error ];
 }
 [localFileManager release];

Refer to this link for the answer:

How to delete the contents of the Documents directory (and not the Documents directory itself)?

Hope this helps all those who are looking out for a solution to this :)

Upvotes: 6

Related Questions