Search code examples
iphoneiosios4

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?


Solution

  • 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
        ...
    }