<!--
{
  "availability" : [
    "macOS: 10.8.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocument/backupFileURL",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Property",
  "symbol" : {
    "kind" : "Instance Property",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocument(py)backupFileURL"
  },
  "title" : "backupFileURL"
}
-->

# backupFileURL

The URL for the document’s backup file that was created during an autosave operation.

```
nonisolated var backupFileURL: URL? { get }
```

## Discussion

This property specifies the location of the backup file, if any. If a backup file cannot be created or is not needed, the value of this property is `nil`.

Starting in OS X v10.8, document versions can be preserved using a backup file created during an autosave operation, which supports document versioning. This property gives you access to the backup file’s URL.

Using an autosave backup file for preserving versions is efficient. This is because an [`NSDocument`](/documentation/AppKit/NSDocument) instance is able to use the <doc://com.apple.documentation/documentation/Foundation/NSFileVersion/ReplacingOptions/byMoving> option when it calls the <doc://com.apple.documentation/documentation/Foundation/NSFileVersion/replaceItem(at:options:)> method. The document gets the value of this property twice during saving:

1. Before calling the [`writeSafely(to:ofType:for:)`](/documentation/AppKit/NSDocument/writeSafely(to:ofType:for:)) method: This is to check whether using the replace-by-moving option is possible and, if not, to allow the system to preserve data by instead using copying.
2. Within the [`writeSafely(to:ofType:for:)`](/documentation/AppKit/NSDocument/writeSafely(to:ofType:for:)) method: This is to discover where to put the backup file.

When you implement the [`writeSafely(to:ofType:for:)`](/documentation/AppKit/NSDocument/writeSafely(to:ofType:for:)) method with the [`NSDocument.SaveOperationType.saveOperation`](/documentation/AppKit/NSDocument/SaveOperationType/saveOperation) or [`NSDocument.SaveOperationType.autosaveInPlaceOperation`](/documentation/AppKit/NSDocument/SaveOperationType/autosaveInPlaceOperation) operation type, you must check this property’s value. If it is not `nil`, move the previous contents of the file (that would be overwritten) to the URL’s location. The default implementation of [`writeSafely(to:ofType:for:)`](/documentation/AppKit/NSDocument/writeSafely(to:ofType:for:)) does this.

To create a backup file from within your custom implementation of the [`writeSafely(to:ofType:for:)`](/documentation/AppKit/NSDocument/writeSafely(to:ofType:for:)) method, call the <doc://com.apple.documentation/documentation/Foundation/FileManager> method <doc://com.apple.documentation/documentation/Foundation/FileManager/replaceItem(at:withItemAt:backupItemName:options:resultingItemURL:)>, using a backup item name of `[[self backupFileURL] lastPathComponent]` and an option of <doc://com.apple.documentation/documentation/Foundation/FileManager/ItemReplacementOptions/withoutDeletingBackupItem> option. If your custom implementation is unable to keep the backup file, you must override this property and return `nil` to ensure that the document’s file gets correctly preserved before it gets overwritten.

The default implementation of the [`writeSafely(to:ofType:for:)`](/documentation/AppKit/NSDocument/writeSafely(to:ofType:for:)) method returns a non-`nil` value based on the value of `[self fileURL]`, but only if the document’s file needs to be preserved prior to saving or if the [`preservesVersions`](/documentation/AppKit/NSDocument/preservesVersions) method returns <doc://com.apple.documentation/documentation/Swift/false>. Otherwise, it returns `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)