How to delete ALL FILES in a specified directory on the app?
40,498
Solution 1
NSFileManager *fm = [NSFileManager defaultManager];
NSString *directory = [[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"];
NSError *error = nil;
for (NSString *file in [fm contentsOfDirectoryAtPath:directory error:&error]) {
BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:@"%@%@", directory, file] error:&error];
if (!success || error) {
// it failed.
}
}
I leave it up to you to do something useful with the error if it exists.
Solution 2
if you want to remove files and the directory itself then use it without for
loop
NSFileManager *fm = [NSFileManager defaultManager];
NSString *directory = [[self documentsDirectory] stringByAppendingPathComponent:@"Photos"];
NSError *error = nil;
BOOL success = [fm removeItemAtPath:cacheImageDirectory error:&error];
if (!success || error) {
// something went wrong
}
Solution 3
same for swift lovers:
let fm = FileManager.default
do {
let folderPath = "...my/folder/path"
let paths = try fm.contentsOfDirectory(atPath: folderPath)
for path in paths
{
try fm.removeItem(atPath: "\(folderPath)/\(path)")
}
} catch {
print(error.localizedDescription)
}
Solution 4
Most of the older answers have you use contentsOfDirectoryAtPath:error:
which will work, but according to Apple:
"The preferred way to specify the location of a file or directory is to use the NSURL class"
so if you want to use NSURL instead you can use the method contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:
so it would look something like this:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray<NSURL*> *urls = [fileManager contentsOfDirectoryAtURL:directoryURL includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
for (NSURL *url in urls)
{
NSError *error = nil;
BOOL success = [fileManager removeItemAtURL:url error:error];
if (!success || error) {
// something went wrong
}
}
Related videos on Youtube
Comments
-
RexOnRoids about 4 years
Given a directory
[[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"]
how do I delete ALL FILES in this folder?(assume a correct documents directory path)
-
zekel over 12 yearsIn general, you should probably use
stringByAppendingPathComponent
instead ofstringWithFormat
to concatenate paths. (I know the above works, but only because of your hard-coded trailing slash in@"Photos/"
.) -
karlbecker_com almost 11 yearsZekel is correct - it's important to use
stringByAppendingPathComponent
, especially if you are, say, using it with iOS. -
Danny over 7 yearsActually the above code as it stands does not work because
stringByAppendingPathComponent
strips the hardcoded slash from@"Photos/"
so that when you try to usedirectory
variable later, it does not include the slash that should separate it fromfile
. A quick fix is to remove the hardcoded trailing slash when creating thedirectory
variable and adding the slash into thestringWithFormat
call between%@%@
-
strikerdude10 over 7 yearsYou probably want to move
[fm contentsOfDirectoryAtPath:directory error:&error]
out of thefor
loop so it doesn't call that method on each iteration. Something likeNSArray<NSString*> *contents = [fm contentsOfDirectoryAtPath:documentsPath error:&error];
andfor (NSString *file in contents)
-
iOS Geek over 6 yearsWorked for me Thanks just needed a little modification, Thanks
-
Andrew Vergunov over 6 yearsIt shouldn't be force after try, if you are using catch. Also, you're trying to remove file by URL, not all files by path.
-
strikerdude10 about 6 yearsFrom Apple: "Attempting to predicate behavior based on the current state of the file system or a particular file on the file system is not recommended", meaning you shouldn't check if the file exists before removing it. You should just try and delete the file and then handle the error appropriately. Check out the documentation on
fileExists(atPath:)
for more info.