<!--
{
  "availability" : [
    "macOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "FSKit",
  "identifier" : "/documentation/FSKit/FSItem/tryReclaim(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "FSKit"
    ],
    "preciseIdentifier" : "c:objc(cs)FSItem(im)tryReclaimWithBlock:"
  },
  "title" : "tryReclaim(_:)"
}
-->

# tryReclaim(_:)

Reclaims the item by executing the given block, if conditions allow.

```
func tryReclaim(_ reclaimBlock: @escaping () -> Void) -> Bool
```

## Return Value

`true` (Swift) or YES (Obj-C)  if the reclaim block ran; otherwise, `false` (Swift) or `NO` (Obj-C). When your [`reclaimItem(_:replyHandler:)`](/documentation/FSKit/FSVolume/Handler/reclaimItem(_:replyHandler:)) implementation receives a `false`/`NO` return value, call `replyHandler(nil)`.

## Discussion

Invoke this method in your implementation of the [`reclaimItem(_:replyHandler:)`](/documentation/FSKit/FSVolume/Handler/reclaimItem(_:replyHandler:)) operation.

FSKit internally maintains a count of how many times it returns each [`FSItem`](/documentation/FSKit/FSItem) to the kernel, via either a creation operation or a lookup. The kernel file system also maintains a count of how many times a create or a lookup operation returned a vnode. When the kernel reclaims the vnode associated with an FSItem, the FSItem is only eligible for reclaiming when both the kernel and user space counts agree.
This mechanism addresses a potential race condition in which concurrent reclaim and lookup operations might lead to a lookup returning a deallocated [`FSItem`](/documentation/FSKit/FSItem), and as a result, induce undefined behavior.
File systems that don’t invoke this method during reclaim are exposed to this race condition.

> Important: The caller must invoke this method within a synchronization context that ensures the ``doc://FSKit/documentation/FSKit/FSItem`` isn’t concurrently returned by lookup operations.

Example Usage:

```swift
func reclaimItem(_ item: FSItem,
                 replyHandler reply: @escaping @Sendable ((any Error)?) -> Void) {
    var reclaimError: NSError? = nil // To be set during the reclaim block in case of an error.

    // *** CRITICAL SECTION BEGINS HERE ***
    // (A synchronization context that ensures the FSItem isn't concurrently returned by lookup operations)

    // Calling `tryReclaim(_:)` with the cleanup logic within the passed block
    let wasReclaimed = item.tryReclaim( {
        // Closure includes all required cleanup operations for reclaiming this item.
        // Sets `reclaimError` in case of an error during the cleanup phase.
    } )

    // *** CRITICAL SECTION ENDS HERE ***

    if (wasReclaimed) {
        // Clean up the FSItem if special teardown is needed.
        reply(reclaimError)
    } else {
        // Do nothing; the FSItem wasn't reclaimed, so it's not yet time to run cleanup.
        reply(nil)
    }
}
```

---

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)