<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Combine",
  "identifier" : "/documentation/Combine/AnyCancellable",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine14AnyCancellableC"
  },
  "title" : "AnyCancellable"
}
-->

# AnyCancellable

A type-erasing cancellable object that executes a provided closure when canceled.

```
final class AnyCancellable
```

## Overview

Subscriber implementations can use this type to provide a “cancellation token” that makes it possible for a caller to cancel a publisher, but not to use the [`Subscription`](/documentation/Combine/Subscription) object to request items.

An [`AnyCancellable`](/documentation/Combine/AnyCancellable) instance automatically calls [`cancel()`](/documentation/Combine/Cancellable/cancel()) when deinitialized.

## Topics

### Creating a type-erased cancellable

[`init(_:)`](/documentation/Combine/AnyCancellable/init(_:)-3icn3)

Initializes the cancellable object with the given cancel-time closure.

[`init(_:)`](/documentation/Combine/AnyCancellable/init(_:)-48fh3)

### Storing instances

[`store(in:)`](/documentation/Combine/AnyCancellable/store(in:)-6cr9i)

Stores this type-erasing cancellable instance in the specified collection.

[`store(in:)`](/documentation/Combine/AnyCancellable/store(in:)-3hyxs)

Stores this type-erasing cancellable instance in the specified set.

## Relationships

### Conforms To

[`Equatable`](/documentation/Swift/Equatable)

[`Cancellable`](/documentation/Combine/Cancellable)

[`Hashable`](/documentation/Swift/Hashable)

---

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)