How to Clear User Data in your Android Application programmatically

Published by Igor Khrupin on

How to clear user data of your application programmatically? Here I share with you my solution how to do this.

You also can clear your user data by clicking “Clear Data” button in Settings–>Applications–>Manage Aplications–> YOUR APPLICATION

To do it programmatically you need to remove user data from application dir.

Below you can download source code of sample Android project.

Pay attention in MyApplication class:

package com.hrupin.cleaner;

import java.io.File;

import android.app.Application;
import android.util.Log;

public class MyApplication extends Application {
	private static MyApplication instance;

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
	}
	
	public static MyApplication getInstance(){
		return instance;
	}
	
	public void clearApplicationData() {
		File cache = getCacheDir();
		File appDir = new File(cache.getParent());
		if(appDir.exists()){
			String[] children = appDir.list();
			for(String s : children){
				if(!s.equals("lib")){
					deleteDir(new File(appDir, s));
					Log.i("TAG", "File /data/data/APP_PACKAGE/" + s +" DELETED");
				}
			}
		}
	}
	
	public static boolean deleteDir(File dir) {
	    if (dir != null && dir.isDirectory()) {
	        String[] children = dir.list();
	        for (int i = 0; i < children.length; i++) {
	            boolean success = deleteDir(new File(dir, children[i]));
	            if (!success) {
	                return false;
	            }
	        }
	    }

	    return dir.delete();
	}
}
Download it from github

28 Comments

Mark waugh · 21 November, 2011 at 06:40

Thanks for this informative post. I’m using Android and using “Clear Data” to clear user data. But programmatically option is new to me and I’ll try it on. 🙂

Remote Data Centre · 22 November, 2011 at 06:46

Oh this is sweet i know how to program a little bit. and this information will come in handy in the future.

Damin martin · 29 November, 2011 at 11:03

Cool buddy!! I’m following exactly same way you describe to clear my Android user data. It’s really good and thanks for informative info.

Gagan Singh · 6 December, 2011 at 09:28

I Was used this code in my personal code but data is not cleared properly some data is not cleared when i checked in settings->application->manage application->app_name.
Every time near about 4 kb data is remaining ,idon’t know why?
please help……………….

Igor · 21 December, 2011 at 19:41

Hi, Gagan Singh
Are your application store all data in /data/data/ dir?

I think it may happens if some of your app data stores in SD card.

zeus · 10 January, 2012 at 14:44

Thanks , I’ve been looking for this exactly .

It comes very useful when performing clean-ups on Android JUnit Test.

Thanks again 😀

Hoffi · 29 March, 2012 at 08:28

Hi Igor,

thank you for this very informative post. I was searching for a way to clear the app data programmatically and you give me the solution 😉

Jorge Pizarro · 3 June, 2012 at 00:10

is there a way to clear the data programatically, but leave out shared preferences untouched? thanks in advance

sreelal · 17 January, 2014 at 07:09

Hi,

is there any way to clear other applications app data or uninstall app silently ….i dont want clear my current app data ,need to clear other app data…

    Igor Khrupin · 8 May, 2014 at 16:03

    Hi sreelal,
    I think this is impossible to make clear data or remove other application without special permissions.
    I know about some special relations between application which signed with same certificate.

antony · 13 March, 2014 at 12:08

very nice forum
thanks.

chillsakoon · 28 May, 2014 at 09:47

Dear is there need any special permission in manifist file? for clear data, cache

Aleration · 7 July, 2014 at 11:57

Very Good! I need this source… Thank you very very much!

Thit Oo · 26 August, 2014 at 13:48

it was many useful for me .
Thank a lot

Gonzalo · 18 November, 2014 at 08:25

What should you put in the asterisks? (**********)

    Igor Khrupin · 18 November, 2014 at 14:06

    Nothing. I’ve updated post for better understanding

      Gonzalo · 19 November, 2014 at 01:04

      Yes!. thank you very much

Peter · 10 December, 2014 at 09:48

Thank you so much for this nice post. A question: Why must the class extend Application? An Explanation would be great!

harman · 20 January, 2017 at 10:07

how can we clear cache in api 23

mona · 22 June, 2017 at 14:38

Hi khrupin, your article is great that helps me 🙂 .
How to know the size of the app data before i clear them programatically?

drapoel · 2 April, 2018 at 19:01

Thanks for the article
But is there a way to Clear all Data in all applications and not in one application
and thank you

    Igor Khrupin · 17 April, 2018 at 13:16

    Hi drapoel,
    There is no way for clear data in all applications.
    Because of security reason.
    But it can be possible if your device is rooted.

    Igor Khrupin · 17 April, 2018 at 13:24

    Hi drapoel,
    There is no way for clear data in all applications.
    It is security reason.
    But it can be possible if your device is rooted.

      jefferson · 5 June, 2018 at 14:49

      assuming it is rooted. what would be the code to delete other application data? thanks.

      jefferson · 5 June, 2018 at 14:51

      if it is rooted what would be the exact code?

        Igor Khrupin · 28 June, 2018 at 09:32

        I don’t have exact code, but idea the same you should go to `/data/data/` folder and clear the data.
        I’ve never done it before. Let me know how it works on your side.
        Thanks

Leave a Reply to Igor Khrupin Cancel reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.