# [android] 캐시데이터 삭제하는 방법 ( delete app cache)
2020. 3. 5. 13:22ㆍAndroid
앱 개발시에 서버 데이터를 변경했는데 적용이 안되는 경우
이 경우에는 앱의 캐시를 삭제
예제
/**
* deleteCache
* @param context
*/
fun deleteCache(context: Context) {
try {
val cache: File = context.cacheDir
val appDir = File(cache.parent)
if (appDir.exists()) {
val children: Array<String> = appDir.list()
for (s in children) {
//다운로드 파일은 지우지 않도록 설정
if (s == "lib" || s == "files") continue
deleteDir(File(appDir, s))
}
}
}catch (e:Exception){
Log.e("DELETE_CACHE" , e.toString())
}
}
fun deleteDir(dir: File?): Boolean {
return if (dir != null && dir.isDirectory) {
val children = dir.list()
for (i in children.indices) {
val success = deleteDir(File(dir, children[i]))
if (!success) {
return false
}
}
dir.delete()
} else if (dir != null && dir.isFile) {
dir.delete()
} else {
false
}
}