2014年4月2日 星期三

Deleting all the files in the iPhone sandbox (documents folder)?

http://stackoverflow.com/questions/4793278/deleting-all-the-files-in-the-iphone-sandbox-documents-folder

Is there an easy way to delete all the files(images) I saved in the documents folder of the app?
share|improve this question
    
stackoverflow.com/questions/4792663/… this is a similar question assuming he wants to delete everything in the documents folder, but no solution yet –  Jinah Adam Jan 25 '11 at 12:12
add comment
up vote71down voteaccepted
NSFileManager *fileMgr = [[[NSFileManager alloc] init] autorelease];
NSError *error = nil;
NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
if (error == nil) {
for (NSString *path in directoryContents) {
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];
BOOL removeSuccess
= [fileMgr removeItemAtPath:fullPath error:&error];
if (!removeSuccess) {
// Error handling
...
}
}
} else {
// Error handling
...
}
share|improve this answer
1  
it worked for the most part. but it turns out it removeItemAtPath requires full file path. so i used this NSString *myFilePath = [documentsDir stringByAppendingPathComponent:path]; thanks. –  Jinah Adam Jan 25 '11 at 12:43
1  
That's right, Jinah. My error. I am gonna correct the code above. –  Ole Begemann Jan 25 '11 at 12:45
2  
If this works, it would be nice to remove the Untested code: comment. Thanks –  Warpspace Feb 9 '12 at 8:58
    
I get an EXC_BAD_ACCESS every time I execute this. But it deletes the contents of my sand box though. How do I prevent the error? Any ideas? –  acecapades Aug 10 '12 at 7:50
1  
I got documentsDirectory as undeclared Identifier. using NSDocumentDirectory also does not work in its place. It says, incompatible integer to pointer conversion. –  Ankit Tanna Aug 21 '13 at 8:07
add comment
- (void)removeFile
{
// you need to write a function to get to that directory
NSString *filePath = [self getDirectory];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath])
{
NSError *error;
if (![fileManager removeItemAtPath:filePath error:&error])
{
NSLog(@"Error removing file: %@", error);
};
}
}
I believe this is shorter.
share|improve this answer
    
It appears this only removes a single file or directory –  Warpspace Feb 9 '12 at 8:55
add comment
Code did not work with IOS 7 and Xcode 5 so edited to work with my app. Big credits to @Ole Begemann and @pablasso.
-(void)EmptySandbox
{
NSFileManager *fileMgr = [[NSFileManager alloc] init];
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];

while (files.count > 0) {
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
if (error == nil) {
for (NSString *path in directoryContents) {
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];
BOOL removeSuccess
= [fileMgr removeItemAtPath:fullPath error:&error];
files
= [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];
if (!removeSuccess) {
// Error
}
}
} else {
// Error
}
}
}
share|improve this answer
    
If you're using CoreData, just note this will delete your Model also –  BenB Mar 20 at 23:35
add comment
 NSFileManager *fileMgr = [[[NSFileManager alloc] init] autorelease];
NSError *error = nil;
NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
if (error == nil) {
for (NSString *path in directoryContents) {
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];
BOOL removeSuccess
= [fileMgr removeItemAtPath:fullPath error:&error];
if (!removeSuccess) {
// Error handling
...
}
}
} else {
// Error handling
...
}

沒有留言:

張貼留言