The other solutions only delete surface level, this will iteratively tear into sub-directories and purge them as well.
Additionally, some answers are using removeItem:
with the local path of just the file itself instead of the full path which is what is required by the OS to properly remove.
Just call [self purgeDocuments];
+(void)purgeDocuments __deprecated_msg("For debug purposes only") {
NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
[self purgeDirectory:documentDirectoryPath];
}
+(void)purgeDirectory:(NSString *)directoryPath __deprecated_msg("For debug purposes only") {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:directoryPath error:&error];
for (NSString *itemPath in directoryContent) {
NSString *itemFullPath = [NSString stringWithFormat:@"%@/%@", directoryPath, itemPath];
BOOL isDir;
if ([fileManager fileExistsAtPath:itemFullPath isDirectory:&isDir]) {
if (isDir) {
[self purgeDirectory:itemFullPath];//subdirectory
} else {
[fileManager removeItemAtPath:itemFullPath error:&error];
}
}
}
}