How Do I Clean Up The FutureAccessList?

Q: How Do I Clean Up The FutureAccessList?

The FutureAccessList is used for retaining permissions when accessing IStorageItems during the lifetime of the process and also between sessions. It has a limit of 1000 entries, however sometimes it’s easy to lose track of entries and fail to remove items that no longer exist or are no longer needed from the list. This results in an exception when you try to add more items than the 1000 item limit. The list does not clean itself up but the following code snippet can remove items that no longer exist:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var fal = StorageApplicationPermissions.FutureAccessList;
var tokenList = fal.Entries.Select(entry => entry.Token).ToList();

foreach (var token in tokenList)
{
    try
    {
        // Will throw if it no longer exists
        await fal.GetItemAsync(token);
    }
    catch (Exception)
    {
        fal.Remove(token);
    }
}