How to Remove Tmp Directory Files of an iOS App

When does iOS clean the local app ./tmp directories?

According to the documentation, it could be any time, if the app is not executing:

The system may also purge lingering files from this directory when your app is not running.

It's logical to assume if a) your app is not running; and b) your device is running low on storage, then it's highly likely the system will, at some point, purge the contents of <Application_Home>/tmp/.

It's also worth noting that the documentation states:

Your app should remove files from this directory when it determines they are no longer needed.

The emphasis here being on the app developer to do their own housekeeping and not leave it for the OS to tidy up after them.

When do photos expire on iOS in /tmp/ folder

You have no control over the lifetime of files in the tmp/ folder. All you know is that the OS may purge such files any time your app is not running.

From the documentation:

Put temporary data in the tmp/ directory. Temporary data comprises any data that you do not need to persist for an extended period of time. Remember to delete those files when you are done with them so that they do not continue to consume space on the user’s device. The system will periodically purge these files when your app is not running; therefore, you cannot rely on these files persisting after your app terminates.

How long do files stay in the documents directory (iOS)

Files in the documents directory will persist until either the user deletes the app, resets their device or you remove the files in code. Also note however that the contents of the documents directory are backed up by iTunes so could persist longer than you would otherwise expect.

On the other hand, the tmp directory is not so persistent and its contents won't necessarily survive an app relaunch (they're also not backed up by iTunes) so you could use this if you don't want persistant storage. /Library/Caches/ is similar in that it isn't backed up but it is persistent. From the docs regarding tmp:

The system will periodically purge these files [in the tmp folder] when your app is not running; therefore, you cannot rely on these files persisting after your app terminates.

Basically, if you want to store something for a short period and it doesn't matter if it's deleted by the OS, use tmp. If you want something that will persist app launches and whatnot but still won't be backed up via iTunes, use the cache folder in Library. It you want something that persists and is backed up, use documents.

Extra info based on comment

If the images should disappear when the app is relaunched then you should use tmp. But take note that the tmp directory is not guaranteed to be cleared on an app relaunch. It's certainly possible that the images will still be there the next time you load the app. It's a bit of a gamble really. If it is vital to kill the images then wipe them manually somewhere suitable:

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

Deleting files from Documents Directory

Problem solved!

My functions are working fine. I found that every picked photo (from UIPicker) is saved in tmp folder in my app.

A solution to this is cleaning tmp folder:
[How to remove tmp directory files of an ios app?

delete temp directory of an app in iOS using cordova plugin

Here is the sample code snippet to delete directory and its contents using cordova file plugin:

function clearDirectory() {
if (ionic.Platform.isAndroid()) {
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemDirSuccess, fail);
} else {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemDirSuccess, fail);
}

function onFileSystemDirSuccess(fileSystem) {
var entry = "";
if (ionic.Platform.isAndroid()) {
entry = fileSystem;
} else {
entry = fileSystem.root;
}
entry.getDirectory("DIRECTORY_TO_DELETE", {
create: true,
exclusive: false
},
function(entry) {
entry.removeRecursively(function() {
console.log("Remove Recursively Succeeded");
}, fail);
}, getDirFail);
}

function getDirFail(error) {
navigator.notification.alert("Error");
};

function fail(error) {
navigator.notification.alert("Error");
};
}


Related Topics



Leave a reply



Submit