1

We have rebuilt our app from the ground up with all new functionality and want to remove all of the previous data when the user upgrades to the new version.

Complication: in the previous app, file names and database names were dynamically generated off user account values, so we need to remove them without knowing their exact names.

Complication #2: The previous app was done by an outside agency. I don't know everything they saved and where. So I'm looking to go nuclear here.

Would this do it? Am I missing anything? Is there a better way? Thanks in advance!

private const val LEGACY_PREFS = "old_shared_prefs"

fun deleteLegacyAppData(context: Context) {
    context.cleanUpLegacySharedPrefs()
    
    // this call assumes that the new code base
    // with the same package name would retrieve
    // the same filesDir
    context.cleanUpLegacyData(context.filesDir)
}

private fun Context.cleanUpLegacySharedPrefs() {
    val prefs = getSharedPreferences(LEGACY_PREFS, Context.MODE_PRIVATE)
    prefs.edit().clear().apply()
}

private fun Context.cleanUpLegacyData(file: File) {
    if (file.isDirectory) {
        for (f in file.listFiles() ?: emptyArray()) {
            cleanUpLegacyData(f)
        }
    }
    file.delete()
}
Psest328
  • 6,575
  • 11
  • 55
  • 90
  • "Would this do it?" -- we have no way of knowing, as we do not know what you stored originally. "Am I missing anything?" -- databases, `SharedPreferences` other than the one that you are handling, and `getCacheDir()` come to mind with respect to [internal storage](https://commonsware.com/blog/2019/10/06/storage-situation-internal-storage.html), but we do not know if you used any of that. "Is there a better way?" -- there is [a `deleteRecursively()` extension function on `File`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-file/delete-recursively.html) that you could use. – CommonsWare Jun 23 '21 at 22:22
  • @ArtemViter "Page Not Found" – Psest328 Jun 24 '21 at 13:25
  • @CommonsWare Well, there's my problem. The previous app was done by an outside agency. I don't know everything they did. So I'm looking to go nuclear here. Clear everything possible – Psest328 Jun 24 '21 at 13:27
  • @Psest328 sorry , try look at https://stackoverflow.com/questions/6134103/clear-applications-data-programmatically – Artem Viter Jun 24 '21 at 13:35
  • @ArtemViter I saw this but apparently it kills the app, which we can't have happen, so I'm looking for a way to do it manually – Psest328 Jun 24 '21 at 13:36
  • @Psest328 Did you see all answers? for example by `MinceMan` – Artem Viter Jun 24 '21 at 14:27

0 Answers0