Search code examples
androidinternal-storage

Are Internal Storage Files Deleted on App Update?


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) 

Solution

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