---
title: try_copy_file function
description: The try\_copy\_file function in Databricks SQL and Databricks Runtime copies a file to a destination path and returns a FILE reference, or NULL if the source file doesn't exist or is inaccessible.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/functions/try_copy_file
---

# `try_copy_file` function

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 18 LTS and above

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md).

Copies a file to a destination path and returns a `FILE` reference, or `NULL` if the source file doesn't exist or is inaccessible. Omit `destination` to copy the file into Unity Catalog-managed storage, which converts a `FILE EXTERNAL` reference to a `FILE MANAGED` reference.

## Syntax

```
try_copy_file(file => file
  [, destination => destination ]
  [, if_file_exists_mode => mode ])
```

You can pass arguments positionally or by name. After you pass an argument by name, all following arguments must also be passed by name. For more information, see [named parameter invocation](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-function-invocation.md#named-parameter-invocation).

## Arguments

* `file`: A `FILE` value to copy.
* `destination`: An optional `STRING` with the full destination file path, not just a directory. When omitted, the file is copied into Unity Catalog-managed storage, converting a `FILE EXTERNAL` reference to a `FILE MANAGED` reference.
* `if_file_exists_mode`: An optional `STRING` that sets the behavior when a file already exists at the destination path. Applies only when using the `destination` argument. Accepted values (case insensitive) are:
  * `'error'`: Raises an error. This is the default value.
  * `'overwrite'`: Overwrites the existing file.
  * `'skip'`: Skips copying and returns a `FILE` reference to the existing file.

## Returns

A `FILE` value that references the copied file, or `NULL` if the source file doesn't exist or is inaccessible.

## Notes

* This function is the error-tolerant version of [`copy_file` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/copy_file.md). Use it to ignore errors when converting a `FILE EXTERNAL` reference to a `FILE MANAGED` reference, whether the conversion is explicit (omitting `destination`) or automatic (inserting a `FILE EXTERNAL` value into a `FILE MANAGED` column).
* If a file already exists at the destination path, Databricks raises an error by default unless you set `if_file_exists_mode` to `overwrite` or `skip`.

## Common error conditions

* `COPY_FILE_ERROR.FILE_ALREADY_EXISTS`
* `COPY_FILE_AUTHORIZATION_ERROR.WRITE_UNAUTHORIZED`

For more information, see [Error conditions in Databricks](https://docs.databricks.com/gcp/en/error-messages/error-classes.md).

## Examples

If the source file doesn't exist, `try_copy_file` returns `NULL`:

```sql
SELECT try_copy_file(deleted_file, destination => '/Volumes/archive/reports/report.pdf');
```

```text
NULL
```

To copy a file into Unity Catalog-managed storage, converting a `FILE EXTERNAL` reference to a `FILE MANAGED` reference, omit the destination:

```sql
SELECT try_copy_file(to_file('/Volumes/source/data/input.csv'));
```

To copy a file and overwrite any existing destination file:

```sql
SELECT try_copy_file(
  to_file('/Volumes/source/reports/report.pdf'),
  destination => '/Volumes/archive/reports/report.pdf',
  if_file_exists_mode => 'overwrite'
);
```

## Related functions

* [`to_file` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/to_file.md)
* [`try_to_file` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/try_to_file.md)
* [`create_file` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/create_file.md)
* [`copy_file` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/copy_file.md)
* [`list_files` table-valued function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/list_files.md)
* [`read_files` table-valued function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/read_files.md)
* [`FILE` type](https://docs.databricks.com/gcp/en/sql/language-manual/data-types/file-type.md)
