내 앱의 캐쉬 데이터 지우기 예제 입니다.

getCacheDir() 에서부터 정보를 불러와서 Folder 스캔하여 아래에 있는 파일을 삭제 하는 방식입니다.

적당히 남겨두어야 하는 부분은 주석을 참조하여 사용하시면 됩니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * 앱 캐시 지우기
 * @param context
 */
public static void clearApplicationData(Context context) {
    File cache = context.getCacheDir();
    File appDir = new File(cache.getParent());
    if (appDir.exists()) {
        String[] children = appDir.list();
        for (String s : children) {
            //다운로드 파일은 지우지 않도록 설정
            //if(s.equals("lib") || s.equals("files")) continue;
            deleteDir(new File(appDir, s));
            Log.d("test", "File /data/data/"+context.getPackageName()+"/" + s + " DELETED");
        }
    }
}
 
private static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return dir.delete();
}


+ Recent posts