Search code examples
objective-ccocoa

how to delete all files in a folder, but not the folder itself?


I try to delete all files including subdirectories in a folder:

NSFileManager *deleteMgr = [NSFileManager defaultManager];
NSString *path = @"~/test/";
[deleteMgr removeItemAtPath:path error:&error];

And it deletes everything including the folder itself. But what I expect is an empty folder. Could anyone please help me what did I do wrong?


Solution

  • You should call contentsOfDirectoryAtPath on the directory in question, and then use removeItemAtPath on all of the paths it returns.