Instance Method

removeItem(atPath:)

Removes the file or directory at the specified path.

Declaration

func removeItem(atPath path: String) throws

Parameters

path

A path string indicating the file or directory to remove. If the path specifies a directory, the contents of that directory are recursively removed. You may specify nil for this parameter.

Return Value

true if the item was removed successfully or if path was nil. Returns false if an error occurred. If the delegate stops the operation for a file, this method returns true. However, if the delegate stops the operation for a directory, this method returns false.

Discussion

Prior to removing each item, the file manager asks its delegate if it should actually do so. It does this by calling the fileManager(_:shouldRemoveItemAt:) method; if that method is not implemented (or the process is running in OS X 10.5 or earlier) it calls the fileManager(_:shouldRemoveItemAtPath:) method instead. If the delegate method returns true, or if the delegate does not implement the appropriate methods, the file manager proceeds to remove the file or directory. If there is an error removing an item, the file manager may also call the delegate’s fileManager(_:shouldProceedAfterError:removingItemAt:) or fileManager(_:shouldProceedAfterError:removingItemAtPath:) method to determine how to proceed.

Removing an item also removes all old versions of that item, invalidating any URLs returned by the url(forPublishingUbiquitousItemAt:expiration:) method to old versions.

See Also

Creating and Deleting Items

func createFile(atPath: String, contents: Data?, attributes: [FileAttributeKey : Any]?) -> Bool

Creates a file with the specified content and attributes at the given location.

func removeItem(at: URL)

Removes the file or directory at the specified URL.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software