<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Dictionary/removeValue(forKey:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SD11removeValue6forKeyq_Sgx_tF"
  },
  "title" : "removeValue(forKey:)"
}
-->

# removeValue(forKey:)

Removes the given key and its associated value from the dictionary.

```
@discardableResult mutating func removeValue(forKey key: Key) -> Value?
```

## Parameters

`key`

The key to remove along with its associated value.

## Return Value

The value that was removed, or `nil` if the key was not
present in the dictionary.

## Discussion

If the key is found in the dictionary, this method returns the key’s
associated value. On removal, this method invalidates all indices with
respect to the dictionary.

```
var hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]
if let value = hues.removeValue(forKey: "Coral") {
    print("The value \(value) was removed.")
}
// Prints "The value 16 was removed."
```

If the key isn’t found in the dictionary, `removeValue(forKey:)` returns
`nil`.

```
if let value = hues.removeValue(forKey: "Cerise") {
    print("The value \(value) was removed.")
} else {
    print("No value found for that key.")
}
// Prints "No value found for that key."
```

> Complexity: O(*n*), where *n* is the number of key-value pairs in the
> dictionary.

---

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)