Using ADB or Appium, programmatically remove linked account from Android Settings

Viewed 1649

How can I programmatically remove/clear a linked account from an Android device within Android Settings > Accounts? Is this possible with ADB or Appium, or by some other programmatic method?

Android devices typically keep linked accounts for Google or Facebook on a device settings level, not within a single app's cache. I would like to remove these accounts (especially Facebook, which appears to only have one account per device).

The context for the question is in automated testing.

  • Removing accounts manually/by hand is not an option.
  • I'd prefer not to do it via Appium UI automation; even if Appium could solve this problem on one device, different android devices/OS versions have different settings UI, thus UI automation is not a scalable solution.
  • The best, easiest, and most scalable solution would allow me to do an ADB command which could remove the linked account.

Edit:
Here is an unanswered question on Appium forums asking a similar question: https://discuss.appium.io/t/android-how-to-remove-google-accounts-linked-a-device-on-setting-activity/6920

2 Answers

Linked accounts are stored in the database /data/system_ce/0/accounts_ce.db, to access it you need root access.

In the case if you have root access you can simply remove the entry of the specified account from the database.

I had the same question, and found something to remove ALL accounts. Take a look at what AuthenticatorDescription types you are getting if you strictly only want to delete e.g. Google Accounts

private void clearAccounts() {
        AccountManager manager = AccountManager.get(getApplicationContext());
        AuthenticatorDescription[] authTypes = manager.getAuthenticatorTypes();
        for (AuthenticatorDescription authDesc : authTypes) {
            Account[] accounts = manager.getAccountsByType(authDesc.type);
            if (accounts.length == 0) {
                continue;  // No accounts of this type, continue loop.
            }
            for (final Account account : accounts) {
                manager.removeAccount(account, null, null, null);
            }
        }
    }
Related