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

Jinah Adam picture Jinah Adam · Jan 25, 2011 · Viewed 33.9k times · Source

Is there an easy way to delete all the files(images) I saved in the documents folder of the app?

Answer

Ole Begemann picture Ole Begemann · Jan 25, 2011
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
    ...
}