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()
}