Skip to content

cleanUpExpiredTemporaryFiles ​

Clean Up Expired Temporary Files ​

The cleanUpExpiredTemporaryFiles method is used to maintain the disk health of the SDK. It scans specific storage paths to identify and delete temporary resources that have not been accessed beyond a specified "retention time".

This operation is executed asynchronously. It is recommended to call it while the app is idle or not running a game, to avoid consuming I/O bandwidth.


Method Declaration ​

java
/**
 * Clean up expired temporary files
 * * @param keepTimeInMinute Retention time of temporary files, in minutes.
 * Calculation rule: if (current time - file's last access time) > keepTimeInMinute, delete the file.
 * @param listener         Status listener, may be null
 */
void cleanUpExpiredTemporaryFiles(
    int keepTimeInMinute, 
    SUDRT.CleanUpExpiredTemporaryFilesListener listener
);

Parameters ​

1. keepTimeInMinute (retention time) ​

  • Type: int
  • Description: Defines the criterion for a file being "expired".
  • Suggested common values:
  • 60: Keep files accessed within the last 1 hour.
  • 1440: Keep files accessed within the last 24 hours (1 day).
  • 10080: Keep files accessed within the last 7 days.

2. CleanUpExpiredTemporaryFilesListener ​

Used to track the progress and result of the cleanup task:

Callback MethodTrigger Timing
onCleanUpStart()Called when the cleanup task officially starts.
onRemove(String path, Throwable exception)Called each time a file or folder is successfully deleted, or when a deletion error occurs.
onSuccess()The entire scan and cleanup task completed successfully.
onFailure(Throwable throwable)The cleanup task was interrupted by a serious error (such as permission being revoked).

Runtime Behavior ​

  1. Scan scope: The SDK traverses the temporary storage paths specified in init or the Bundle, such as KEY_RUNTIME_STORAGE_PATH_CACHE.
  2. Access time check: The comparison is based on the file system's lastAccessTime (last access time).
  3. Recursive cleanup: If all files in a folder have expired, the SDK attempts to delete the empty folder along with them.
  4. Yield mechanism: If a file is currently in use (for example, the game is running and reading that resource), the deletion operation may skip the file or report it via exception.

Example ​

java
// Set to keep temporary files accessed within the last 24 hours
int keepOneDay = 24 * 60; 

runtime.cleanUpExpiredTemporaryFiles(keepOneDay, new SUDRT.CleanUpExpiredTemporaryFilesListener() {
    @Override
    public void onCleanUpStart() {
        Log.d("SUDOP", "Disk cleanup started...");
    }

    @Override
    public void onRemove(String path, Throwable exception) {
        if (exception == null) {
            Log.d("SUDOP", "Removed: " + path);
        } else {
            Log.e("SUDOP", "Failed to remove: " + path + ", error: " + exception.getMessage());
        }
    }

    @Override
    public void onSuccess() {
        Log.i("SUDOP", "Cleanup finished successfully.");
    }

    @Override
    public void onFailure(Throwable throwable) {
        Log.e("SUDOP", "Cleanup failed: " + throwable.getMessage());
    }
});

Notes ​

  • Performance impact: A large-scale file scan generates disk I/O. Calling it while a game is loading or running may cause frame drops.
  • Irreversibility: Once a file is deleted it cannot be recovered; it must be downloaded or generated again.
  • Use together: If you need to abort the cleanup task immediately, call cancelCleanUp() alongside it.