How to Clear User Data in your Android Application programmatically

How to clear user data of your application programmatically? Here I share 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
Application Info Android Dialog

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 source code. Here you can download sample project.

facebooktwittergoogle_plusredditpinterestlinkedintumblrmailby feather

13 thoughts on “How to Clear User Data in your Android Application programmatically

  1. 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. :)

  2. 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……………….

  3. Thanks , I’ve been looking for this exactly .

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

    Thanks again :D

  4. 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 ;)

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

  6. 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…

    • 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.

Leave a Reply

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


one + = 5

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>