2

Introduction
in my project, i've to delete folder and it's content(s) so i tried with this accepted answer ClickHere

it works and i thought that the task is over but after deleting a whole folder(Directory)i see that the memory is still allocated but file doesn't exist.
here is my code to delete directory(folder).

-(BOOL)removeItem:(NSString*)name{
    //name: is directory(folder)'s name
    NSString*path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    path = [path stringByAppendingPathComponent:@"Normal"];
    path = [path stringByAppendingPathComponent:name];
    NSError* err;
    NSArray* list = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:path error:&err];
    if(err){
        NSLog(@"\n##CONTENTS OF DIRECTORY ERROR:->\n%@",err.localizedDescription);
    }
    //if array's count is not zero(0) it means directory has files available.
    if(list.count >= 1){
        for(NSString* string in list){
            [[NSFileManager defaultManager]removeItemAtPath:[path stringByAppendingPathComponent:string] error:&err];
            if(err){
                NSLog(@"\n##FOLDER IN FOLDER ERROR:->\n%@",err.localizedDescription);
            }
        }
        [[NSFileManager defaultManager]removeItemAtPath:path error:&err];
        return YES;
    }else{
        [[NSFileManager defaultManager]removeItemAtPath:path error:&err];
        if (err) {
            return NO;
        }
        return YES;
    }
}


thanks in advance.

Community
  • 1
  • 1
Vatsal Shukla
  • 1,274
  • 12
  • 25
  • 1
    Please explain what you mean by "memory is still allocated". – A-Live Oct 05 '15 at 11:01
  • @A-Live, when i install my app in real device. when fresh installation, the size of app is around 6mb shown in setting->General->Usage->Manage Storage. than i add some files(video files) and size increases. but when i delete some files(video files) (yes, files ware removed), it seems size of my app doesn't decrease. – Vatsal Shukla Oct 05 '15 at 12:27
  • It is possible that the space is actually freed but the stats aren't updated immediately, it might take some time to refresh automatically. Also could be that the stats are cached and need to be somehow reset, if they don't have expiration date restarting Settings or the device might help. If you have evidences of the filesystem not letting you create new files due to lack of space and you believe that is happening because the space isn't actually freed, please add more details about how you came to this conclusion. – A-Live Oct 05 '15 at 13:26

1 Answers1

7

Alright, i got the issue.in my case the issue was a temp folder(directory). when i add video to my app, iOS created some temporary files in app's temp folder(directory). so, app's size i seen in setting->General->Usage->Manage Storage after removing the files(videos) from my app , was the size of temp folder.
so, when you import photos or videos from Gallery you have to manually clear the temp folder after fetching. i'm clearing the temp folder from UIImagePickerController's delegate as bellow.

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
[self dismissViewControllerAnimated:YES completion:^{
BOOL success = [videoData writeToFile:path atomically:YES];
            NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
            [RWTAppDelegate clearTmpDirectory];
}];
}


+ (void)clearTmpDirectory
{
    NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
    for (NSString *file in tmpDirectory) {
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
    }
}


Update for swift 3+

class func clearTmpDirectory(){
        let path = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
        let manager = FileManager.default
        let files = try? manager.contentsOfDirectory(atPath: path.path)
        files?.forEach { (file) in
            let temp = path.appendingPathComponent(file)
            try? manager.removeItem(at: temp)
            // --- you can use do{} catch{} for error handling ---//
        }
}


sorry for my bad english.

Vatsal Shukla
  • 1,274
  • 12
  • 25
  • 1 up! would be better if left it here: - (void)applicationWillTerminate:(UIApplication *)application {} – Jiulong Zhao Dec 27 '15 at 00:21
  • @JiulongZhao, yes, you are right but in my case this creates storage space issues after some time because i have to add video frequently, and i can't wait for "-(void)applicationWillTerminate:(UIApplication*)application{}" – Vatsal Shukla Dec 30 '15 at 09:24
  • Thanks a lot. Really annoying the UIImagePickerController doesn't clean up after it self but it makes sense it doesn't because the files returned are stored in the NSTemporaryDirectory. I would recommend not deleting the NSTemporaryDirectory files until applicationWillTerminate as I've found out the hard way, deleting these files early the image picker still returns the delete file paths. – harmeet07 Oct 20 '17 at 16:08