MuntashirAkon/AppManager · error · AndroidException

Could not clear cache of package

Error message

Could not clear cache of package ${pair}

What it means

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.

Solutions

  1. Catch AndroidException and handle the failure gracefully (inform user / retry).
  2. Ensure the caller holds DELETE_CACHE_FILES and CLEAR_APP_USER_DATA permissions or runs with elevated privileges.
  3. Verify the package is installed for the target user first.
  4. Use non-privileged alternatives (e.g. StorageManager cache APIs or direct cache-dir deletion for your own app).

Example fix

// before
PackageManagerCompat.deleteApplicationCacheFilesAsUser(pkg, userId);
// after
try {
    PackageManagerCompat.deleteApplicationCacheFilesAsUser(pkg, userId);
} catch (AndroidException e) {
    Log.w(TAG, "Cache clear failed for " + pkg + ": " + e.getMessage());
}
Defensive patterns

Strategy: try-catch

Validate before calling

try { pm.getPackageInfoAsUser(pkg, 0, userId); } catch (PackageManager.NameNotFoundException e) { return; }
int granted = ContextCompat.checkSelfPermission(ctx, Manifest.permission.DELETE_CACHE_FILES);
if (granted != PackageManager.PERMISSION_GRANTED) return;

Try / catch

try {
    PackageManagerCompat.deleteApplicationCacheFilesAsUser(pkg, userId);
} catch (AndroidException e) {
    Log.w(TAG, "Cache clear failed: " + e.getMessage());
}

Prevention

When it happens

Trigger: Calling deleteApplicationCacheFilesAsUser(packageName, userId) where the system denies the operation (missing DELETE_CACHE_FILES / CLEAR_APP_CACHE privileges) or the binder callback never completes.

Common situations: 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.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/a1f8433f240e76e6. Report an issue: GitHub.

Appendix: source

Thrown at app/src/main/java/io/github/muntashirakon/AppManager/compat/PackageManagerCompat.java:591

            "android.permission.INTERNAL_DELETE_CACHE_FILES"
    })
    public static void deleteApplicationCacheFilesAsUser(UserPackagePair pair) throws AndroidException {
        IPackageManager pm = getPackageManager();
        ClearDataObserver obs = new ClearDataObserver();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            pm.deleteApplicationCacheFilesAsUser(pair.getPackageName(), pair.getUserId(), obs);
        } else pm.deleteApplicationCacheFiles(pair.getPackageName(), obs);
        //noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (obs) {
            while (!obs.isCompleted()) {
                try {
                    obs.wait(500);
                } catch (InterruptedException ignore) {
                }
            }
        }
        if (!obs.isSuccessful()) {
            throw new AndroidException("Could not clear cache of package " + pair);
        }
        BroadcastUtils.sendPackageAltered(ContextUtils.getContext(), new String[]{pair.getPackageName()});
    }

    @RequiresPermission(allOf = {
            Manifest.permission.DELETE_CACHE_FILES,
            "android.permission.INTERNAL_DELETE_CACHE_FILES"
    })
    public static boolean deleteApplicationCacheFilesAsUser(String packageName, int userId) {
        try {
            deleteApplicationCacheFilesAsUser(new UserPackagePair(packageName, userId));
            return true;
        } catch (AndroidException | SecurityException e) {
            e.printStackTrace();
            return false;
        }
    }

View on GitHub (pinned to 0152f468fc)