Are Internal Storage Files Deleted on App Update?
Asked Answered
P

3

7

It is known that:

When the user uninstalls your app, the system removes all your app's files from internal storage.

Does the same thing happens when the user update the app?


Why Am I asking? I have a file in the raw/ folder which I move to the Internal Storage every time my app is run (if it is not already been moved):
//On Application onCreate() method:
InputStream jsonFile = ctx.getResources().openRawResource(R.raw.data);
File file = ctx.getFileStreamPath(Helper.FILE_NAME);
if(file.exists())
    return; //Exit because the file is already been copied.
FileOutputStream fos = ctx.openFileOutput(Helper.FILE_NAME, Context.MODE_PRIVATE);
copyFile(fos, jsonFile); //my method to copy files.

The file might be updated in future releases of the app. if the System deletes internal files upon updating the app, the above code will be Ok. However, if this is not the case, then I need to implement another check to ensure the user has the latest version. Something like this:

if(file.exists() && fileVersion == lastVersion) 
Portulaca answered 26/8, 2015 at 15:49 Comment(7)
I would still have the extra checks, just in case. Can never be too sure with the bloody IO system! :-)Netsuke
@Netsuke Yet having an unnecessary IO read on every app run seems wasteful too!Portulaca
It will just be called onCreate()/onResume() -- nothing wasteful about making sure the app will not crash!Netsuke
You won't lose internal files on update. However, you should always have checks anyway; what if the user decides to delete the cache manually?Alysaalyse
@Knossos, then the file won't be there, and it will be copied from raw. But placing an app/file version in shared prefs or (better) a database isn't very expensive.Tails
but the Prefs file version will need to be compared with the file current version, hence the extra IO read.Portulaca
possible duplicate of Android Internal Storage when updating applicationNetsuke
T
12

Internal files are removed only if you uninstall and reinstall. An upgrade will not remove internal files -- and it shouldn't. Consider that if it did that, your database would be removed for every upgrade as well.

You will need to add an additional check.

For reference, the following documentation explains this in a roundabout way:

Storage Options

Note that the first paragraph:

Android provides several options for you to save persistent application data.

(emphasis added)

Further down under Internal Storage it then says

When the user uninstalls your application, these files are removed.

By omission I conclude that since these options are "persistent" and it's only explicitly stated that the files will be removed on uninstall, that an upgrade will retain those files.

I've never seen an internal file removed during upgrade, so experientially, this holds true as well.

For what it's worth, if the file you have in raw is read-only, you don't even need to copy it. openRawResource() will give you an InputStream and allow you to access it as any other file. Internally, it's just that: A file.

Copying to external storage isn't a good alternative as the user (and in earlier Android versions, any other app) can access, delete or modify that file.

Tails answered 26/8, 2015 at 15:58 Comment(5)
In support of his deduction, you should take a look at the conversation in this answer(the one I've linked above): https://mcmap.net/q/835217/-android-internal-storage-when-updating-applicationNetsuke
@Sipty, I don't see how that answer adds anything, honestly.Tails
I always assumed that when the system upgrades the app, then it will uninstall+install the app, and uninstalling means deleting the app data. That's why it was confusing to me.Portulaca
@iturki, that's understandable. One of my app keeps a very large pool of internal data (2-3GB) which always has to be available to the user. Further, it's sensitive data, which can not be out in the open. If that was removed with each upgrade, it would be a painful user experience, and possibly result in outlandish data charges. This app has been used since Android 2.2, and has seen countless upgrades since, with the internal files never compromised.Tails
@Tails somehow users of my app keep complaining loosing data after upgrade, though I couldn't get root cause. May be its that users are uninstalling and installing, as its happening often I am moving to external storage optionsSlunk
H
2

Uninstall will only remove internal files bundled with the application, folders create in the external storage will not be deleted when uninstall. for database, the database will be deleted when uninstall but will remain for updated application.

Still on the database, its depends on the way the developer built the application, if there is a change of database version, there can be a replace or a complete removal of the database.

Henrion answered 26/8, 2015 at 16:38 Comment(2)
We are talking about updating, not uninstalling the app.Portulaca
thought i should give explanation on uninstalling as well, since there is a similar process involve.Henrion
N
0

By using external storage, i.e. /sdcard/, your data will not be deleted, even when the app's being uninstalled.

Regarding internal storage: The question has been asked before: Android Internal Storage when updating application

And lastly, I would not leave anything to chance -- always make sure to check!

Netsuke answered 26/8, 2015 at 15:58 Comment(6)
An explanation to why I a perfectly valid answer is being down voted would be helpful.Netsuke
Maybe someone is downvote happy... mine (the correct answer) got downvoted too. I could see it on yours as it doesn't actually answer the question. The question was about internal storage, not external storage, which you have addressed correctly. As such, this isn't an answer, and I could see why it was downvoted.Tails
Yes, I just reread the question and am in the process of updating it. : )Netsuke
@Netsuke Thanks for sharing the link. Regarding the external storage, it is not an option unfortunately.Portulaca
If you found a duplicate question, you should mark this question as such and keep Stackoverflow clean (and noise free)Tails
I don't see anything noisy about giving a detailed answer, outlining alternatives.Netsuke

© 2022 - 2024 — McMap. All rights reserved.