---
title: cancel()
framework: foundation
role: symbol
role_heading: Instance Method
path: foundation/urlsessiontask/cancel()
---

# cancel()

Cancels the task.

## Declaration

```swift
func cancel()
```

## Discussion

Discussion This method returns immediately, marking the task as being canceled. Once a task is marked as being canceled, urlSession(_:task:didCompleteWithError:) will be sent to the task delegate, passing an error in the domain NSURLErrorDomain with the code NSURLErrorCancelled. A task may, under some circumstances, send messages to its delegate before the cancelation is acknowledged. This method may be called on a task that is suspended.

## See Also

### Related Documentation

- [cancel(byProducingResumeData:)](foundation/urlsessiondownloadtask/cancel(byproducingresumedata:).md)

### Controlling the task state

- [resume()](foundation/urlsessiontask/resume().md)
- [suspend()](foundation/urlsessiontask/suspend().md)
- [state](foundation/urlsessiontask/state-swift.property.md)
- [URLSessionTask.State](foundation/urlsessiontask/state-swift.enum.md)
- [priority](foundation/urlsessiontask/priority.md)
- [URL session task priority](foundation/url-session-task-priority.md)
