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 Overflow
🌐
Quora
quora.com › How-can-I-delete-temp-files-in-Android
How to delete temp files in Android - Quora
Answer (1 of 4): 1 Open the Settings app on your Android device. You can usually find the Settings app in your App Drawer, or by pressing your device's Menu button and selecting Settings. You can also pull down from the notifications shade to get to settings. 2 Tap "Apps" or "Applications". Th...
Discussions

java - Android Memory -- Temporary Files in Cache or Put on Disk, Delete When Done - Stack Overflow
I am writing an app which records and plays audio using a temporary file on the device and longer-term storage on the cloud, so it only needs to be available on the physical device when directly in... More on stackoverflow.com
🌐 stackoverflow.com
caching - Cache and temp files/folders remove android - Stack Overflow
Hi friend thanks for previous replies, i am facing problem in removing cache and temp files/folders, what i require is to clean the whole device temp files and cache from one app which is mine app ... More on stackoverflow.com
🌐 stackoverflow.com
How to delete Temporary System Files from Pixel 7 - Google Pixel Community
Due to the latest device launch, we expect to receive higher contact volume than normal. To check if your question is already answered, go to the Pixel Phone Help Center More on support.google.com
🌐 support.google.com
April 12, 2025
android - Is it programmer responsibility to delete temp file - Stack Overflow
Currently, I have an application, which will save a temporary screenshot, and let user share it through facebook. The image saving code is more or less as follow. // Use external cache dir, as our... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › 3a713a50aff6c63aa3771387e9935375
Clear Android Studio Temp Files · GitHub
Clear Android Studio Temp Files · Raw · gistfile1.txt · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Google Support
support.google.com › files › answer › 9713869
Clear your junk files - Files by Google Help
Some apps store temporary files. You can free up space on your device by clearing these files. App settings are not affected. Important: If you clear junk files or delete files using Files by Google, the data will be permanently deleted. On your Android device, open Files by Google .
🌐
Stack Overflow
stackoverflow.com › questions › 37582356 › android-memory-temporary-files-in-cache-or-put-on-disk-delete-when-done
java - Android Memory -- Temporary Files in Cache or Put on Disk, Delete When Done - Stack Overflow
Thanks to the comments by Gusman I used the first approach to create a temporary file and included code to clean up the local files once they are loaded onto the backend server or in the event the user closes the app without uploading the recording (onDestroy()). Is it programmer responsibility to delete temp file
🌐
Quora
quora.com › How-do-I-delete-temporary-files-in-Android
How to delete temporary files in Android - Quora
Answer (1 of 3): To delete temporary files on Android, go to Settings → Storage → Cached data and tap Clear cache. You can also open Settings → Apps, select an app, tap Storage, then choose Clear cache. This removes temporary files without deleting your personal data.
Find elsewhere
🌐
Mkyong
mkyong.com › home › java › how to delete a temporary file in java
How to delete a temporary file in Java - Mkyong.com
July 21, 2020 - In Java, we can use the NIO Files.delete() or Files.deleteIfExists() to delete a temporary file, it works the same like delete a regular text file.
🌐
Mobikin
mobikin.com › data-clean › clear-android-temp-files.html
[Full Guide] How to Delete Temp Files on Android Phones?
May 11, 2023 - Click Clear Cache and Clear Data to remove temporary app files on Android. It's also vital to delete temp files on your Android/Samsung phone or tablet from the menu of Browser.
🌐
Google Support
support.google.com › pixelphone › thread › 337815737 › how-to-delete-temporary-system-files-from-pixel-7
How to delete Temporary System Files from Pixel 7 - Google Pixel Community
April 12, 2025 - Due to the latest device launch, we expect to receive higher contact volume than normal. To check if your question is already answered, go to the Pixel Phone Help Center
🌐
Sourceht
sourceht.com › home › how to delete temporary files on android
How to Delete Temporary Files on Android in Some Easy Steps
May 31, 2021 - Enter the “Settings” menu of your Android. Navigate to “Applications and notifications” and locate the app whose cache you want to empty. Click on “Storage and cache” and select “Clear cache.” · Repeat this same process with ...
🌐
Medium
droidbyme.medium.com › clear-cache-in-android-programmatically-26a1405a470b
Clear Cache in Android programmatically | by Droid By Me | Medium
August 28, 2018 - public static void deleteCache(Context ... { return dir.delete(); } else { return false; } } By clicking on clear cache button in your code, call deleteCache(this) method....
🌐
Android Guías
en.androidguias.com › android guides › android tutorials › complete guide to deleting temporary files and cache on android without losing data
How to clear cache and temporary files on Android step by step
April 29, 2025 - Choose "Clear cache"This process will only delete temporary files, keeping your user data, photos, and accounts intact. Important: Don't choose the "Clear storage" or "Clear data" option unless you want to restore the app to its newly installed ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-delete-temporary-file-in-java
How to Delete Temporary File in Java? - GeeksforGeeks
August 5, 2021 - You can use deleteOnExit() method to delete an existing file only when your application completes. If your code terminates abnormally then keep in mind that your temporary file has not been deleted yet and once you have requested for the delete ...
🌐
Samsung
samsung-messages-backup.com › resources › clear-temp-files-android.html
Fast Clear Temp Files on Android Devices (6 Approaches)
This app is free for Android users and easy to use. Step 1: Install then start the Files by Google app on your device, and click on the "Clean" button from the bottom left. Step 2: Go to the "Junk Files" tab and click on "Confirm". Then click on the "See Junk Files" icon.
🌐
Reddit
reddit.com › r/motorola › need help with deleting temp system files without resetting my phone
r/motorola on Reddit: Need help with deleting temp system files without resetting my phone
May 12, 2025 -

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.