How to Clear Android Application User Data Programmatically

How to Clear Android Application User Data Programmatically

22 April 2024 Stephan Petzl Leave a comment Tech-Help

Developers often need to clear an application’s user data during development, testing, or resetting an application to its default state. This can be done using the Android Debug Bridge (ADB), a command-line tool that allows for communication with an Android device.

The most straightforward way to clear application data is by using the following ADB command:

adb shell pm clear

Replace with the package name of the Android application whose data you want to clear.

Programmatic Approach to Clearing App Data

If you need to clear user data from within an Android application, you’ll have to use a different approach. The following example demonstrates how to programmatically clear an application’s data:

String packageName = "";
Runtime runtime = Runtime.getRuntime();
try {
    runtime.exec("pm clear " + packageName);
} catch (IOException e) {

Note that this requires the android.permission.CLEAR_APP_USER_DATA permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA"/>

However, due to security restrictions, this permission is not available to third-party applications. As such, the above approach will not work for non-system apps without rooting the device.

Alternative Methods for Clearing App Data

For clearing data of other apps, your app needs to be signed with the system firmware key or installed as a privileged app on rooted devices. In such cases, you might execute a command with superuser privileges:

Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "pm clear"});

Remember that rooting your device and using superuser privileges can compromise the security and void the warranty of the device.

Using Repeato for Clearing App Data During Automated Testing

When conducting automated testing, it’s essential to start with a clean state for each test to ensure reliability. Repeato, our no-code test automation tool, excels in creating, running, and maintaining automated tests for your apps on iOS and Android.

Repeato can be particularly helpful in automating the process of clearing application data before each test run. With its built-in ADB capabilities, you can insert script steps to execute ADB commands, such as clearing app data, ensuring that each test case starts with a fresh state.

This feature not only streamlines the testing process but also removes the manual overhead of resetting application states, making it an efficient solution for maintaining test consistency.

To learn more about how Repeato can optimize your test automation, visit our product page at Repeato vs. Appium.

Like this article? there’s more where that came from!