开发者

How to delete the contents of the Documents directory (and not the Documents directory itself)?

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开发者_如何学Python that lets you delete the contents of a directory only and leaving the empty directory there?


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];
}


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)
}


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)
    }
}


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];
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜