caches directory counts towards app storage?

throughout all of Foundation's URL documentation, its called out in multiple places that data stored inside an app sandox's caches directory doesn't count towards data and documents usage in the settings app

but in practice, it looks like storing data there does in fact count towards documents & data for the app

i'm trying to understand if the docs are wrong, if theres a bug in the settings app, or if this is a mistake on my part

i've created an app that measures the cache directory, and has a button to write 10MB of random data into the caches directory

  1. after installing the app, documents and data say 0
  2. after hitting the write to cache button in the app, documents and data reports count(button_tap)*10 MB.
  3. this is the buggy behavior. it should still report 0MB

i've created an app that measures the cache directory, and has a button to write 10MB of random data into the caches directory

  1. after installing the app, documents and data say 0
  2. after hitting the write to cache button in the app, documents and data reports count(button_tap)*10 MB.
  3. this is the buggy behavior. it should still report 0MB
//
//  ContentView.swift
//  cache-example
//
//  Created by Andrew Breckenridge on 12/4/24.
//

import SwiftUI

struct ContentView: View {
    @State private var cacheSize: String = "Cache Size: 0 MB"
    
    var body: some View {
        VStack(spacing: 20) {
            Text(cacheSize)
                .font(.system(size: 22, weight: .medium))
            
            Button(action: writeRandomDataToCache) {
                Text("Write 10MB to Cache")
                    .padding()
                    .background(Color.gray)
                    .foregroundColor(.white)
                    .cornerRadius(8)
            }
        }
        .padding()
        .onAppear(perform: updateCacheSize)
    }
    
    private func writeRandomDataToCache() {
        let data = Data(count: 10 * 1024 * 1024) // 10MB of random data
        if let cacheDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first {
            let fileURL = cacheDirectory.appendingPathComponent(UUID().uuidString)
            do {
                try data.write(to: fileURL)
                updateCacheSize()
            } catch {
                print("Error writing data to cache: \(error)")
            }
        }
    }
    
    private func updateCacheSize() {
        if let cacheDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first {
            do {
                let files = try FileManager.default.contentsOfDirectory(at: cacheDirectory, includingPropertiesForKeys: [.fileSizeKey], options: .skipsHiddenFiles)
                let totalSize = files.reduce(0) { (result, fileURL) -> Int in
                    let fileSize = (try? fileURL.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0
                    return result + fileSize
                }
                cacheSize = "Cache Size: \(totalSize / (1024 * 1024)) MB"
            } catch {
                print("Error calculating cache size: \(error)")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Yeah, we did make it clear in this tech talk that:

"As a bonus to you, files in your caches directory are not reported as part of your app's Documents & Data total. Although they are included in the overall space taken up by apps."

Now that you did see that the number listed in General > iPhone Storage > The app > Documents & Data includes the files in the caches folder, would you mind to file a feedback report with your demo project to see what the team has to say? If you do so, please share your report ID here. Thanks.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

caches directory counts towards app storage?
 
 
Q