<!--
{
  "availability" : [
    "iOS: 4.0.0 -",
    "iPadOS: 4.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.6.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSPurgeableData",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSPurgeableData"
  },
  "title" : "NSPurgeableData"
}
-->

# NSPurgeableData

A mutable data object containing bytes that can be discarded when they’re no longer needed.

```
class NSPurgeableData
```

## Overview

[`NSPurgeableData`](/documentation/Foundation/NSPurgeableData) objects inherit their creation methods from their superclass, [`NSMutableData`](/documentation/Foundation/NSMutableData) while providing a default implementation of the [`NSDiscardableContent`](/documentation/Foundation/NSDiscardableContent) protocol.

All [`NSPurgeableData`](/documentation/Foundation/NSPurgeableData) objects begin “accessed” to ensure that they are not instantly discarded. The [`beginContentAccess()`](/documentation/Foundation/NSDiscardableContent/beginContentAccess()) method marks the object’s bytes as “accessed,” thus protecting them from being discarded, and must be called before accessing the object, or else an exception will be raised. This method returns <doc://com.apple.documentation/documentation/Swift/true> if the bytes have not been discarded and if they have been successfully marked as “accessed”. Any method that directly or indirectly accesses these bytes or their length when they are not “accessed” will raise an exception. When you are done with the data, call [`endContentAccess()`](/documentation/Foundation/NSDiscardableContent/endContentAccess()) to allow them to be discarded in order to quickly free up memory.

You may use these objects by themselves, and do not necessarily have to use them in conjunction with [`NSCache`](/documentation/Foundation/NSCache) to get the purging behavior. The [`NSCache`](/documentation/Foundation/NSCache) class incorporates a caching mechanism with some auto-removal policies to ensure that its memory footprint does not get too large.

[`NSPurgeableData`](/documentation/Foundation/NSPurgeableData) objects should not be used as keys in hashing-based collections, because the value of the bytes pointer can change after every mutation of the data.

---

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)