---
title: create_file function
description: The create\_file function in Databricks SQL and Databricks Runtime uploads string or binary content to a destination path and returns a FILE reference to the uploaded file.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/functions/create_file
---

# `create_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).

Uploads string or binary content to a destination path and returns a `FILE` reference to the uploaded file.

## Syntax

```
create_file(content => content
  [, destination_path => path ]
  [, if_file_exists => mode ]
  [, content_type => content_type ])
```

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

* `content`: A `STRING` or `BINARY` expression with the content to store as a file. A `STRING` is stored as UTF-8 encoded bytes.
* `destination_path`: An optional `STRING` path to upload the content to.
* `if_file_exists`: An optional `STRING` that sets the behavior when a file already exists at the destination path. Applies only when using the `destination_path` argument. Accepted values (case insensitive) are:
  * `'error'`: Raises an error. This is the default value.
  * `'overwrite'`: Overwrites the existing file.
  * `'skip'`: Skips the upload and returns a `FILE` reference to the existing file.
* `content_type`: An optional `STRING` that specifies the content type of the file. If not provided, the content type is inferred from the input bytes.

## Returns

A `FILE` value that references the uploaded file.

## Notes

* If you use `destination_path`, the file is uploaded atomically to that path. You must have write access to the target location, which is typically a Unity Catalog volume.
* If a file already exists at the destination path, Databricks raises an error by default unless you set `if_file_exists` to `overwrite` or `skip`.

## Common error conditions

* `CREATE_FILE_ERROR.FILE_ALREADY_EXISTS`
* `CREATE_FILE_AUTHORIZATION_ERROR.WRITE_UNAUTHORIZED`
* `CREATE_FILE_ILLEGAL_USAGE.CONTENT_REQUIRED`
* `CREATE_FILE_ILLEGAL_USAGE.IF_FILE_EXISTS_WITHOUT_DESTINATION`
* `CREATE_FILE_ILLEGAL_USAGE.IF_FILE_EXISTS_MODE`

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

## Examples

To store a string as a new file at a given path:

```sql
SELECT create_file(
  content => 'hello, world',
  destination_path => '/Volumes/my_catalog/my_schema/my_volume/greetings.txt'
);
```

To store raw bytes and set the content type explicitly:

```sql
SELECT create_file(
  content => my_binary_col,
  destination_path => '/Volumes/my_catalog/my_schema/my_volume/image.png',
  content_type => 'image/png'
)
FROM raw_uploads;
```

To skip the upload if a file already exists at the destination:

```sql
SELECT create_file(
  content => my_binary_col,
  destination_path => '/Volumes/my_catalog/my_schema/my_volume/report.pdf',
  if_file_exists => 'skip'
);
```

If `destination_path` isn't set, `if_file_exists` raises an error:

```sql
SELECT create_file(content => 'data', if_file_exists => 'overwrite');
```

```text
Error: CREATE_FILE_ILLEGAL_USAGE.IF_FILE_EXISTS_WITHOUT_DESTINATION
```

## 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)
* [`copy_file` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/copy_file.md)
* [`try_copy_file` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/try_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)
