MuntashirAkon/AppManager · error · AndroidException

Could not clear data of package

Error message

Could not clear data of package ${pair}

What it means

The internal helper that clears application user data spawns an IPackageDataObserver and waits (obs.wait(500)) for the framework callback. If the observer never reports success (clearApplicationUserData failed, was rejected, or never completed), an AndroidException("Could not clear data of package <pair>") is thrown where <pair> describes package/user.

Solutions

  1. Catch AndroidException and surface a failure message / retry option.
  2. Verify the app holds android.permission.CLEAR_APP_USER_DATA (or runs as root/shell).
  3. Confirm the package exists for the target user before clearing.
  4. Increase wait tolerance or handle the observer timeout explicitly if callbacks are slow on the device.

Example fix

// before
PackageManagerCompat.clearApplicationUserData(pkg, userId);
// after
try {
    PackageManagerCompat.clearApplicationUserData(pkg, userId);
} catch (AndroidException e) {
    Log.w(TAG, "Clear data 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, "android.permission.CLEAR_APP_USER_DATA");
if (granted != PackageManager.PERMISSION_GRANTED) return;

Try / catch

try {
    PackageManagerCompat.clearApplicationUserData(pkg, userId);
} catch (AndroidException e) {
    Log.w(TAG, "Clear data failed: " + e.getMessage());
}

Prevention

When it happens

Trigger: Calling clearApplicationUserData(packageName, userId) where the OS refuses or fails the clear (permission denied, package missing, observer timeout without success).

Common situations: Clearing data of another app without CLEAR_APP_USER_DATA/system privileges; package uninstalled mid-operation; the clear callback timing out or never firing on some OEM ROMs.

Related errors


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

Appendix: source

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            try {
                pm.clearApplicationUserData(pair.getPackageName(), obs, pair.getUserId(), false);
            } catch (NoSuchMethodError e) {
                // Fall back to the old API
                pm.clearApplicationUserData(pair.getPackageName(), obs, pair.getUserId());
            }
        } else pm.clearApplicationUserData(pair.getPackageName(), obs, pair.getUserId());
        //noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (obs) {
            while (!obs.isCompleted()) {
                try {
                    obs.wait(500);
                } catch (InterruptedException ignore) {
                }
            }
        }
        if (!obs.isSuccessful()) {
            throw new AndroidException("Could not clear data of package " + pair);
        }
        BroadcastUtils.sendPackageAltered(ContextUtils.getContext(), new String[]{pair.getPackageName()});
    }

    @RequiresPermission(ManifestCompat.permission.CLEAR_APP_USER_DATA)
    public static boolean clearApplicationUserData(@NonNull String packageName, @UserIdInt int userId) {
        try {
            clearApplicationUserData(new UserPackagePair(packageName, userId));
            return true;
        } catch (AndroidException | SecurityException e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequiresPermission(allOf = {
            Manifest.permission.DELETE_CACHE_FILES,
            "android.permission.INTERNAL_DELETE_CACHE_FILES"

View on GitHub (pinned to 0152f468fc)