Delete the files in onDestroy if isChangingConfigurations() is false or isFinishing is true. Example:
@Override protected void onDestroy() {
super.onDestroy();
if(!isChangingConfigurations()) {
deleteTempFiles(getCacheDir());
}
}
private boolean deleteTempFiles(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
deleteTempFiles(f);
} else {
f.delete();
}
}
}
}
return file.delete();
}
Answer from Jared Rummler on Stack OverflowDelete the files in onDestroy if isChangingConfigurations() is false or isFinishing is true. Example:
@Override protected void onDestroy() {
super.onDestroy();
if(!isChangingConfigurations()) {
deleteTempFiles(getCacheDir());
}
}
private boolean deleteTempFiles(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
deleteTempFiles(f);
} else {
f.delete();
}
}
}
}
return file.delete();
}
call the deleteOnExit() method!
Or
call the delete() method in the onStop() of your activity.
Edit:
It might be better if you called delete() in onDestroy() to insure that your code works even if app is destroyed by system.
java - Android Memory -- Temporary Files in Cache or Put on Disk, Delete When Done - Stack Overflow
caching - Cache and temp files/folders remove android - Stack Overflow
How to delete Temporary System Files from Pixel 7 - Google Pixel Community
android - Is it programmer responsibility to delete temp file - Stack Overflow
AFAIK yes, it's your responsibility to delete files not needed anymore (be they temporary or not). OS cannot know how long your app will be using that file.
Its upto each and every application to keep the house (or rather the device) clean. :)
That being said
- The JVM is always running as far as android is booted up and running. So the file should get deleted if the device is restarted.
- There is good chance that the user will get offended if an app is leaving temporary files even when the app has been closed.
So close the temporary file yourself, when you are done with it. Need not even be until the app exits!
I just wasted like 10 minutes going through every app, deleting cache. Manually. Still have 42 GB of temporary system files.
Files doesn't have an option to delete them.
Would I need to root my phone? :(
I think the best way is to delete it in overrided onBackPressed() method of you activity. You see, back pressed event is the only exit point that means that user is done interactig with this activity instance and will newer return to it.
Always use getExtExternalCacheDir() API to get the cache directory on sdcard. Create all your temp files in this path. This cache will be destroyed after your app is uninstalled
Even better use getCacheDir() for cache dir path in local storage.. This memory will be automatically cleared when device is low on memory.
Here is a post that explains how to clear cache manually: How to delete cache-folder of app?
I am using motorola edge 50 pro and I noticed there is so much storage used just for temp files and also there's no option for deleting them as well. I need a solution for optimising my storage and deleting these files.