Search Unity

Unable to clear unused AssetBundles

Discussion in 'Addressables' started by bpapegaaij, Feb 17, 2020.

  1. bpapegaaij

    bpapegaaij

    Joined:
    Mar 24, 2017
    Posts:
    3
    We are trying to use the addressables system to deliver content over-the-air.
    After messing around with the system a bit, we have a working setup using a custom Build script and custom AssetBundleProvider. The latter performs some logic at runtime to fetch a URL (since this requires access tokens etc.) and then calls through to the regular AssetBundleProvider to do the rest of the heavy lifting. This all seems to be working pretty well.

    However, we ran into a problem when trying to minimise that data footprint of the game. In order to try and get rid of stale bundles, we modified our AssetBundleProvider to clear the bundle cache of old versions whenever we've successfully loaded a particular bundle. To do so, we're using the Unity Caching API, as describe elsewhere on this forum specifically:
    Code (CSharp):
    1. Caching.ClearOtherCachedVersions()
    However, when inspecting the cache directory, the cache is never actually deleted.

    When calling
    Code (CSharp):
    1. Caching.GetCachedVersions()
    with the same bundle name, I get a list of versions back, so I'm convinced my bundle name is correct.

    We've also tried using
    Code (CSharp):
    1. Caching.ClearAllCachedVersions()
    with bundles that are definitely not loaded (i.e. an old version of a bundle named with filename + hash), and while it returns success, the bundles are never deleted.

    Is there something I'm not understanding about the Caching API, or is it simply broken? When I set up our addressables with CacheInitializationSettings, the system seems to correctly clear the cache of expired bundles, but manually clearing never works.

    Any help would be greatly appreciated

    Edit:
    We're using addressables version 1.6, and Unity version 2019.2.13f1
     
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,822
    We'll pass this along to the team to review for you.
     
  3. bpapegaaij

    bpapegaaij

    Joined:
    Mar 24, 2017
    Posts:
    3
    Thanks for the response

    I've tried to replicate this behaviour in an empty project and haven't had much luck so far. In my empty project, at least when I call Caching.ClearAllCachedVersions(), the directory containing the data for that bundle seems to be correctly cleared out.

    In our game project however, this is not the case.
    Currently in our project I'm doing the following
    • I call Caching.GetAllCachedVersions(), this gives me back a correct list of the versions, as reflected on disk
    • I call Caching.ClearAllCachedVersions() (same bundle name), this returns true
    • I check the cache directory, all cached versions are right where I left them
    There's a couple of difference that might be notable, but I'm having a bit of difficulty with reproducing due to the slightly complex nature of our Addressables setup
    • In our project, the Caching API is invoked from inside a ResourceProvider's Provide() function
      • This is in contrast to my test project where it happens in a MonoBehaviour, albeit in a callback from an addressables load
    • In our project, the bundle names end up having URL-encoded slashes in them (i.e. "%2F") which is reflected in the cache directory.
      • This is not the case in the test project since it appears to be a bit hard to reproduce that behaviour without a custom build script
    • The bundle names in our main project also contain "."
    Hopefully that information can be of some help, as I can't think of any other differences that would cause different behaviour between the two projects

    Edit: After a bit more digging, it seems clearing the cache works correctly for our local bundles, which don't contain either "%2F" or "." in their path names
     
    Last edited: Feb 21, 2020
  4. bpapegaaij

    bpapegaaij

    Joined:
    Mar 24, 2017
    Posts:
    3
    I've got an update on this issue,

    It seems that the caching behaviour is correct if I just remove the "." characters from the bundle name. Luckily these were only present because our remote buildpath contains the app version, so I can easily replace them with underscores.

    Is it possible the caching API internally does a Path.GetFilenameWithoutExtension(bundleName) (or equivalent) during calls for clearing the cache, but not for other calls?
     
  5. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    Having a quick glance at the engine code, I think you're right. Calling any of the clear cached versions will perform a GetFileNameWithoutExtension but Getting the cached versions doesn't appear to do the same. At least not from what I can tell right away. That's interesting.