15

I want to delete all the files and directories contained in the Documents directory.

I believe using [fileManager removeItemAtPath:documentsDirectoryPath error:nil] method would remove the documents directory as well.

Is there any method that lets you delete the contents of a directory only and leaving the empty directory there?

j0k
  • 22,600
  • 28
  • 79
  • 90
NSExplorer
  • 11,849
  • 12
  • 49
  • 62

4 Answers4

51

Try this:

NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSError *error = nil;
for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error]) {
    [[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:file] error:&error];
}
cmlloyd
  • 975
  • 9
  • 14
4

Swift 3.x

let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
guard let items = try? FileManager.default.contentsOfDirectory(atPath: path) else { return }

for item in items {
    // This can be made better by using pathComponent
    let completePath = path.appending("/").appending(item)
    try? FileManager.default.removeItem(atPath: completePath)
}
footyapps27
  • 3,982
  • 2
  • 25
  • 42
3

I think that working with URLs instead of String makes it simpler:

private func clearDocumentsDirectory() {
    let fileManager = FileManager.default
    guard let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

    let items = try? fileManager.contentsOfDirectory(at: documentsDirectory, includingPropertiesForKeys: nil)
    items?.forEach { item in
        try? fileManager.removeItem(at: item)
    }
}
Jakub Sowa
  • 31
  • 3
0

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];
            }
        }
    }
}
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195