{"record":{"id":"a1f8433f240e76e6","repo":"MuntashirAkon/AppManager","slug":"could-not-clear-cache-of-package-pair","errorCode":null,"errorMessage":"Could not clear cache of package ${pair}","messagePattern":"Could not clear cache of package (.+?)","errorType":"exception","errorClass":"AndroidException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/AppManager/compat/PackageManagerCompat.java","lineNumber":591,"sourceCode":"            \"android.permission.INTERNAL_DELETE_CACHE_FILES\"\n    })\n    public static void deleteApplicationCacheFilesAsUser(UserPackagePair pair) throws AndroidException {\n        IPackageManager pm = getPackageManager();\n        ClearDataObserver obs = new ClearDataObserver();\n        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {\n            pm.deleteApplicationCacheFilesAsUser(pair.getPackageName(), pair.getUserId(), obs);\n        } else pm.deleteApplicationCacheFiles(pair.getPackageName(), obs);\n        //noinspection SynchronizationOnLocalVariableOrMethodParameter\n        synchronized (obs) {\n            while (!obs.isCompleted()) {\n                try {\n                    obs.wait(500);\n                } catch (InterruptedException ignore) {\n                }\n            }\n        }\n        if (!obs.isSuccessful()) {\n            throw new AndroidException(\"Could not clear cache of package \" + pair);\n        }\n        BroadcastUtils.sendPackageAltered(ContextUtils.getContext(), new String[]{pair.getPackageName()});\n    }\n\n    @RequiresPermission(allOf = {\n            Manifest.permission.DELETE_CACHE_FILES,\n            \"android.permission.INTERNAL_DELETE_CACHE_FILES\"\n    })\n    public static boolean deleteApplicationCacheFilesAsUser(String packageName, int userId) {\n        try {\n            deleteApplicationCacheFilesAsUser(new UserPackagePair(packageName, userId));\n            return true;\n        } catch (AndroidException | SecurityException e) {\n            e.printStackTrace();\n            return false;\n        }\n    }\n","sourceCodeStart":573,"sourceCodeEnd":609,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/AppManager/compat/PackageManagerCompat.java#L573-L609","documentation":"deleteApplicationCacheFilesAsUser waits on an IPackageDataObserver for the framework to delete an app's cache files. If the observer never reports success (the operation failed, was rejected, or the callback never arrived), an AndroidException(\"Could not clear cache of package <pair>\") is thrown.","triggerScenarios":"Calling deleteApplicationCacheFilesAsUser(packageName, userId) where the system denies the operation (missing DELETE_CACHE_FILES / CLEAR_APP_CACHE privileges) or the binder callback never completes.","commonSituations":"Clearing another app's cache as a regular (non-system, non-root) app; package removed mid-call; OEM ROMs that silently drop the callback; user/profile mismatch.","solutions":["Catch AndroidException and handle the failure gracefully (inform user / retry).","Ensure the caller holds DELETE_CACHE_FILES and CLEAR_APP_USER_DATA permissions or runs with elevated privileges.","Verify the package is installed for the target user first.","Use non-privileged alternatives (e.g. StorageManager cache APIs or direct cache-dir deletion for your own app)."],"exampleFix":"// before\nPackageManagerCompat.deleteApplicationCacheFilesAsUser(pkg, userId);\n// after\ntry {\n    PackageManagerCompat.deleteApplicationCacheFilesAsUser(pkg, userId);\n} catch (AndroidException e) {\n    Log.w(TAG, \"Cache clear failed for \" + pkg + \": \" + e.getMessage());\n}","handlingStrategy":"try-catch","validationCode":"try { pm.getPackageInfoAsUser(pkg, 0, userId); } catch (PackageManager.NameNotFoundException e) { return; }\nint granted = ContextCompat.checkSelfPermission(ctx, Manifest.permission.DELETE_CACHE_FILES);\nif (granted != PackageManager.PERMISSION_GRANTED) return;","typeGuard":null,"tryCatchPattern":"try {\n    PackageManagerCompat.deleteApplicationCacheFilesAsUser(pkg, userId);\n} catch (AndroidException e) {\n    Log.w(TAG, \"Cache clear failed: \" + e.getMessage());\n}","preventionTips":["Hold DELETE_CACHE_FILES/CLEAR_APP_USER_DATA privileges","Verify package presence for the user","Offer non-privileged fallbacks (own-app cache deletion)"],"tags":["android","clear-cache","package-manager"],"backgroundTag":"operation-failed","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}