<!--
{
  "documentType" : "article",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/checking-volume-storage-capacity",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Checking Volume Storage Capacity"
}
-->

# Checking Volume Storage Capacity

Confirm that you have enough local storage space for a large amount of data.

## Discussion

Before you try to store a large amount of data locally, first verify that you have sufficient storage capacity. To get the storage capacity of a volume, you construct a URL (using an instance of [`URL`](/documentation/Foundation/URL))  that references an object on the volume to be queried, and then query that volume.

### Decide Which Query Type to Use

The query type to use depends on what’s being stored. If you’re storing data based on a user request or resources the app requires to function properly (for example, a video the user is about to watch or resources that are needed for the next level in a game), query against [`volumeAvailableCapacityForImportantUsageKey`](/documentation/Foundation/URLResourceKey/volumeAvailableCapacityForImportantUsageKey). However, if you’re downloading data in a more predictive manner (for example, downloading a newly available episode of a TV series that the user has been watching recently), query against [`volumeAvailableCapacityForOpportunisticUsageKey`](/documentation/Foundation/URLResourceKey/volumeAvailableCapacityForOpportunisticUsageKey).

### Construct a Query

Use this example as a guide to construct your own query:

```swift
let fileURL = URL(fileURLWithPath:"/")
do {
    let values = try fileURL.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey])
    if let capacity = values.volumeAvailableCapacityForImportantUsage {
        print("Available capacity for important usage: \(capacity)")
    } else {
        print("Capacity is unavailable")
    }
} catch {
    print("Error retrieving capacity: \(error.localizedDescription)")
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)