Search code examples
iosfilensfilemanager

NSFileManager removeItemAtPath deletes the entire folder when called more than once


I have the following code which should delete 3 files:

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *pngFilePath = [docDir stringByAppendingPathComponent:currentFileName];
NSLog(@"png %@",pngFilePath);

NSString *thumbFilePath = [docDir stringByAppendingPathComponent:currentThumbFileName];
NSLog(@"thumb %@",thumbFilePath);

NSString *plistFilePath = [docDir stringByAppendingPathComponent:currentPlistName];
NSLog(@"plist %@",plistFilePath);

NSError *error, *error2, *error3;
[[NSFileManager defaultManager] removeItemAtPath:pngFilePath error:&error];
[[NSFileManager defaultManager] removeItemAtPath:thumbFilePath error:&error2];
[[NSFileManager defaultManager] removeItemAtPath:plistFilePath error:&error3];

However, it deletes the whole Documents folder. But If I comment two of the last three lines so it deletes only one of the files, It deletes only that file.

Update: Here's the NSLog:

2013-01-23 13:51:28.715 Amaziograph[16466:c07] png /Users/Hristo/Library/Application Support/iPhone Simulator/5.1/Applications/C9FAA196-7904-4070-A208-A53451A64602/Documents/amaziograph_2013_01_23_13_43_37.png
2013-01-23 13:51:28.716 Amaziograph[16466:c07] thumb /Users/Hristo/Library/Application Support/iPhone Simulator/5.1/Applications/C9FAA196-7904-4070-A208-A53451A64602/Documents/amaziograph_2013_01_23_13_43_37_thumb.png
2013-01-23 13:51:28.716 Amaziograph[16466:c07] plist /Users/Hristo/Library/Application Support/iPhone Simulator/5.1/Applications/C9FAA196-7904-4070-A208-A53451A64602/Documents/amaziograph_2013_01_23_13_43_37.plist

How do I do it delete all 3 of them?


Solution

  • It turned out I was calling the code above twice - the first time without a specific filename so it deleted the file's folder. The NSLog said:

    png /Users/... .../Documents/(null).png
    

    So the Documents folder got deleted. Yet I cannot explain why it worked if only one file was deleted at once.