# \[Android\] how to clear app data before test

**URL:** <https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166>\
**Category:** Support\
**Created:** [November 19, 2015, 9:43am UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166 "2015-11-19T09:43:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![teak](https://discuss.appium.io/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@teak](https://discuss.appium.io/u/teak)\
**Post date:** [November 19, 2015, 9:43am UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/1 "2015-11-19T09:43:32Z")

</div>

Hi, Im pretty new to Appium.  
I want the applications data to be cleared before each test.  
How can I accomplish this?  
I thought this was done by default [“no-reset”," false"];  
but my app starts where it left off each time and I have to manually clear the app data from the application settings on the device.  
Thanks

---

<div class="post-metadata">

**Author:** ![Asi\_Hizkiahu](https://discuss.appium.io/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@Asi\_Hizkiahu](https://discuss.appium.io/u/Asi_Hizkiahu)\
**Post date:** [November 19, 2015, 12:29pm UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/2 "2015-11-19T12:29:39Z")

</div>

You can run the uninstall command line…  
adb uninstall YOUR\_APP\_PACKAGE

---

<div class="post-metadata">

**Author:** ![teak](https://discuss.appium.io/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@teak](https://discuss.appium.io/u/teak)\
**Post date:** [November 19, 2015, 2:01pm UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/3 "2015-11-19T14:01:10Z")

</div>

Thanks for the response Asi,  
I do not want to uninstall the package but clear the data before my test as to always start at the splash screen.  
Thanks

---

<div class="post-metadata">

**Author:** ![wreed](https://discuss.appium.io/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wreed](https://discuss.appium.io/u/wreed)\
**Post date:** [November 19, 2015, 4:48pm UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/4 "2015-11-19T16:48:30Z")

</div>

‘adb shell pm clear my.wonderful.app.package’

---

<div class="post-metadata">

**Author:** ![teak](https://discuss.appium.io/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@teak](https://discuss.appium.io/u/teak)\
**Post date:** [November 19, 2015, 5:25pm UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/5 "2015-11-19T17:25:27Z")

</div>

Thanks wreed,  
is there a way to implement this in a test?  
I was thinking it would be a capability setting;  
e.g. capabilities.setCapability(“no-reset”," false")  
Thanks again.

---

<div class="post-metadata">

**Author:** ![wreed](https://discuss.appium.io/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wreed](https://discuss.appium.io/u/wreed)\
**Post date:** [November 19, 2015, 6:45pm UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/6 "2015-11-19T18:45:50Z")

</div>

Short answer: Yes!

Long answer: Your testing should not be dependent on Appium, but rather on your language of choice and the testing framework that you use. You don’t give either, so I’ll use mine:

I use Ruby, my testing framework is rspec, therefore I create a method in spec\_helper.rb:

```
def clear_data
 `adb shell pm clear my.wonderful.app.package`
end

```

Now in my test I make sure to do this:

```
before(:all) do
 clear_data
end

```

So if I have tests that don’t need to clear data, no problem. Otherwise I’ll clear it before my tests. Note that I could also do a before(:each) if I wanted to clear data between tests in a suite.

Note that this is an oversimplified example to keep the discussion on point.

---

<div class="post-metadata">

**Author:** ![willosser](https://discuss.appium.io/user_avatar/discuss.appium.io/willosser/32/2454_2.png) [@willosser](https://discuss.appium.io/u/willosser)\
**Post date:** [November 19, 2015, 10:28pm UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/7 "2015-11-19T22:28:20Z")

</div>

If you have the right capabilities, Appium will install and start your application before execution and uninstall it afterwards. See [http://appium.io/slate/en/master/?ruby#server-flags](http://appium.io/slate/en/master/?ruby#server-flags) for more details.

In our implementation, we start up the appium server and execute all of our test cases, so each of our test cases controls whether or not they need the data cleared exactly as @wreed described.

The downside of this implementation is that you’ve created your own adb module. If you are going to use a device solution like Sauce Labs or BitBar, they may not have similar facilities to do the same.

---

<div class="post-metadata">

**Author:** ![teak](https://discuss.appium.io/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@teak](https://discuss.appium.io/u/teak)\
**Post date:** [November 20, 2015, 8:57am UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/8 "2015-11-20T08:57:57Z")

</div>

Thanks very much,  
I am using Java with JUnit.  
I could try something similar.  
So you suggest to create a method that executes a shell command before the test?

---

<div class="post-metadata">

**Author:** ![wreed](https://discuss.appium.io/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wreed](https://discuss.appium.io/u/wreed)\
**Post date:** [November 20, 2015, 6:04pm UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/9 "2015-11-20T18:04:12Z")

</div>

Yes, JUnit also provides @BeforeClass. You can specify before all or before each in that framework.

---

<div class="post-metadata">

**Author:** ![lokesh\_sharma](https://discuss.appium.io/user_avatar/discuss.appium.io/lokesh_sharma/32/8505_2.png) [@lokesh\_sharma](https://discuss.appium.io/u/lokesh_sharma)\
**Post date:** [May 21, 2018, 7:03am UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/10 "2018-05-21T07:03:46Z")

</div>

> [@wreed](#):
>
> 'adb shell pm clear my.wonderful.app.pack

Yes, You can Create a class called ADB In ADB class …following are the methods u can create.  
1)Check whether Android Home is available on System environment.  
2)Check the OS part  
3)Execute the command  
4)In command prompt pass Commad + Device id

public static String Get\_Android(){

```
	if(Android_Home==null ) {
		Android_Home=System.getenv("Android_Home");
	if(Android_Home==null) throw new RuntimeErrorException("Failed to find android home","Make sure environment variable is set");
		}
	return Android_Home;
	
}

public static String getos(){
	if(os==null) os= System.getenv("os.name");
	return os;			
}

public static boolean iswindows(){
	return getos().startsWith("Windows");
	
}

// *****************************************************Run command******************************************* 88

public static String runcommand(String command){
	
	String output=null;
	try{
		Scanner scan = new Scanner(Runtime.getRuntime().exec(command).getInputStream()).useDelimiter("//A");			
	}
	catch (IOException e){
		throw new RuntimeException(e.getMessage());	
	}
	return output;		

```

===============================

---

<div class="post-metadata">

**Author:** ![Dmytro\_Berezovskyi](https://discuss.appium.io/user_avatar/discuss.appium.io/dmytro_berezovskyi/32/10097_2.png) [@Dmytro\_Berezovskyi](https://discuss.appium.io/u/Dmytro_Berezovskyi)\
**Post date:** [June 23, 2022, 4:28pm UTC](https://discuss.appium.io/t/android-how-to-clear-app-data-before-test/7166/11 "2022-06-23T16:28:41Z")

</div>

```
 def reset(self: T) -> T:
        """Resets the current application on the device.

        Returns:
            Union['WebDriver', 'Applications']: Self instance
        """
        self.execute(Command.RESET)
        return self
```
