Orphan file deletion for Snowflake-managed Apache Iceberg™ tables

Snowflake can automatically find orphan files in the external storage of your Snowflake-managed Apache Iceberg™ tables, tag them, and, if you choose, delete them. This topic describes how orphan file deletion works and how to enable and configure it.

An orphan file is a file in a table’s storage location that no live version of the table references. For example, a failed DML operation can leave Parquet files in your storage that the table metadata doesn’t track. Orphan files continue to incur storage charges from your cloud storage provider until they’re removed.

How orphan file deletion works

When you enable orphan file deletion for your account, Snowflake periodically does the following for the Snowflake-managed Iceberg tables in the account:

  1. Detect: Snowflake compares the files in your table’s storage location with the files that the table references. A file becomes an orphan file candidate when it’s at least as old as the minimum file age and isn’t referenced by the current table or by any table version within the table’s data retention period (Time Travel).
  2. Tag: Snowflake adds the object tag SnowflakeIcebergOrphan=true to each candidate in your storage.
  3. Wait: The file stays tagged for a cooling period. During this time, you can review the candidate in the ICEBERG_ORPHAN_FILE_CANDIDATES_HISTORY view view.
  4. Act: When the cooling period ends, Snowflake takes the action that you configured:
    • TAG: The file stays tagged in your storage. Snowflake doesn’t delete it.
    • DELETE: Snowflake deletes the file, if it still carries the tag.

By default, Snowflake only tags files and doesn’t delete them.

The following table summarizes the settings that control this process:

ParameterDefaultDescription
ENABLE_MANAGED_ICEBERG_ORPHAN_FILE_DELETIONFALSEOpts your account in to orphan file deletion.
MANAGED_ICEBERG_ORPHAN_FILE_DELETION_ACTIONTAGAction to take on orphan files after the cooling period: TAG or DELETE.
MANAGED_ICEBERG_ORPHAN_FILE_COOLING_PERIOD_IN_DAYS30Number of days between tagging a file and acting on it. Valid values are 7 to 30.
ICEBERG_OFD_MIN_CANDIDATE_FILE_AGE_IN_DAYS14Minimum file age, in days, before Snowflake considers a file an orphan file candidate. Valid values are 14 to 90.

For descriptions of these parameters, see Parameters.

Requirements and limitations

  • Storage: Orphan file deletion supports Snowflake-managed Iceberg tables that use an external volume on Amazon S3 that you own. It doesn’t support external volumes on Google Cloud Storage or Microsoft Azure.
  • File types: Snowflake currently detects, tags, and deletes only Apache Parquet data files. Support for orphaned Iceberg metadata files isn’t available yet.
  • Enablement: Contact Snowflake Support to enable this feature for your account. After Support enables it, you can opt in yourself. For more information, see Enable orphan file deletion.
  • Bucket versioning: Versioning must be enabled on the S3 bucket of every external volume that backs a Snowflake-managed Iceberg table in your account. Snowflake checks this when you opt in and each time it runs. If any one external volume fails the check, Snowflake skips orphan file deletion for all tables in your account until you fix it. This includes a volume that you add after you opt in. Enable versioning on the bucket of a new external volume before you create tables on it. If Snowflake stops reporting new orphan files, check versioning on your buckets. For more information, see Protecting files with versioning and object retention.
  • Tagging permissions: The IAM role of each external volume must grant the object tagging and versioning permissions. For more information, see Grant the required permissions. After you opt in, Snowflake checks the tagging permissions when you create a Snowflake-managed Iceberg table on an S3 external volume, and when you convert a table to Snowflake-managed by using ALTER ICEBERG TABLE ... CONVERT TO MANAGED. The statement fails if the role can’t tag objects. As a result:
    • Grant the permissions to a new external volume before you create or convert tables on it.
    • If you remove the permissions from an existing external volume, CREATE ICEBERG TABLE and ALTER ICEBERG TABLE ... CONVERT TO MANAGED on that volume can fail.

Warning

Snowflake treats any file under the storage location of a Snowflake-managed Iceberg table that the table doesn’t reference as an orphan file candidate. This includes files that you, or an external engine, place under a table location. Don’t store files that you want to keep under the location of a Snowflake-managed Iceberg table. If you set the action to DELETE, Snowflake can delete these files.

Enable orphan file deletion

To enable orphan file deletion, do the following:

  1. Contact Snowflake Support and ask them to enable orphan file deletion for your account.

  2. Make sure versioning is enabled on the S3 bucket of each external volume that backs a Snowflake-managed Iceberg table. For more information, see Protecting files with versioning and object retention.

  3. Grant the permissions that orphan file deletion requires to the IAM role of each external volume. For more information, see Grant the required permissions.

  4. Opt in your account by using the ACCOUNTADMIN role:

    ALTER ACCOUNT SET ENABLE_MANAGED_ICEBERG_ORPHAN_FILE_DELETION = TRUE;
    

    Snowflake checks each external volume before it accepts the setting. The command fails if Snowflake can’t tag objects in the bucket, if versioning isn’t enabled or can’t be read, or if Snowflake can’t restore an object that it deletes in the bucket. The error message lists the external volumes that failed the check.

By default, Snowflake tags orphan files and doesn’t delete them. To delete orphan files, see Configure orphan file deletion.

Grant the required permissions

Orphan file deletion requires permissions beyond those that Snowflake needs to read and write Iceberg tables. If you configured your external volumes before orphan file deletion was available, you must update the IAM policies manually. For the permissions and a sample policy, see Grant permissions for orphan file deletion.

Configure orphan file deletion

You can set the following parameters on your account. Changes take effect on the next run.

Delete orphan files

To delete orphan files after the cooling period, set the action to DELETE:

ALTER ACCOUNT SET MANAGED_ICEBERG_ORPHAN_FILE_DELETION_ACTION = DELETE;

To return to tagging only, set the action to TAG:

ALTER ACCOUNT SET MANAGED_ICEBERG_ORPHAN_FILE_DELETION_ACTION = TAG;

Snowflake reads the action when a file’s cooling period ends, not when it tags the file. As a result:

  • If you change the action from TAG to DELETE, Snowflake deletes files that were tagged earlier and whose cooling period has already ended. Snowflake also deletes files that are still cooling when their cooling period ends.
  • If you change the action from DELETE to TAG, Snowflake stops deleting. Files that reach the end of their cooling period stay tagged.

Note

Before you set the action to DELETE, review the files that Snowflake tagged in the ICEBERG_ORPHAN_FILE_CANDIDATES_HISTORY view view.

Change the cooling period

The cooling period is the number of days between when Snowflake tags a file and when it takes the configured action. The default is 30 days. You can set it to a value from 7 to 30 days:

ALTER ACCOUNT SET MANAGED_ICEBERG_ORPHAN_FILE_COOLING_PERIOD_IN_DAYS = 14;

Change the minimum file age

Snowflake doesn’t consider a file an orphan file candidate until it’s at least 14 days old, so that it doesn’t flag files that are still being written or are about to be referenced. You can increase the minimum file age to a value from 14 to 90 days:

ALTER ACCOUNT SET ICEBERG_OFD_MIN_CANDIDATE_FILE_AGE_IN_DAYS = 30;

You can’t set the minimum file age below 14 days.

Disable orphan file deletion

To stop orphan file deletion for your account, opt out:

ALTER ACCOUNT SET ENABLE_MANAGED_ICEBERG_ORPHAN_FILE_DELETION = FALSE;

Orphan file tag

Snowflake marks each orphan file candidate in your storage with the following S3 object tag:

KeyValue
SnowflakeIcebergOrphantrue

Before Snowflake deletes a file, it confirms that the file still carries this tag. Snowflake uses the tag to identify files that it manages, so you can use it to find tagged files in your bucket.

Warning

Don’t add, change, or remove the SnowflakeIcebergOrphan tag on any object. Changing the tag can cause orphan file deletion to behave in unexpected ways, including leaving files undeleted or deleting files that you didn’t intend to delete. If you think a tag is incorrect, contact Snowflake Support before you make any changes.

Monitor orphan file deletion

Use the following Account Usage views to monitor orphan file deletion:

ViewDescription
ICEBERG_ORPHAN_FILE_CANDIDATES_HISTORY viewOne row for each orphan file candidate that Snowflake detected, with its current status.
ICEBERG_ORPHAN_FILE_DETECTION_HISTORY viewOne row for each detection run, with the number and size of the orphan file candidates.

The views might not show data until after detection runs.

The following example shows the most recent detection runs:

SELECT *
  FROM SNOWFLAKE.ACCOUNT_USAGE.ICEBERG_ORPHAN_FILE_DETECTION_HISTORY
  ORDER BY EVENT_TIMESTAMP DESC
  LIMIT 100;

The following example shows the files that Snowflake skipped and why:

SELECT DATABASE_NAME, SCHEMA_NAME, TABLE_NAME, FILE_PATH, MESSAGE, STATUS_TIME
  FROM SNOWFLAKE.ACCOUNT_USAGE.ICEBERG_ORPHAN_FILE_CANDIDATES_HISTORY
  WHERE STATUS = 'SKIPPED'
  ORDER BY STATUS_TIME DESC
  LIMIT 100;

The following example shows the files that Snowflake scheduled for deletion:

SELECT DATABASE_NAME, SCHEMA_NAME, TABLE_NAME, FILE_PATH, FILE_SIZE_BYTES, STATUS_TIME
  FROM SNOWFLAKE.ACCOUNT_USAGE.ICEBERG_ORPHAN_FILE_CANDIDATES_HISTORY
  WHERE STATUS = 'DELETION_SCHEDULED'
  ORDER BY STATUS_TIME DESC
  LIMIT 100;

Recover deleted files

If you need to restore a deleted file, or you think Snowflake deleted a file that an Iceberg table still references, for example because queries on the table fail, contact Snowflake Support as soon as possible. Versioning on your S3 bucket makes recovery possible, so don’t disable versioning on your bucket.

Billing

Snowflake doesn’t bill for orphan file deletion during the preview. When the feature becomes generally available, Snowflake bills orphan file deletion as compute, under the same Table Optimization line item as other table optimization features, such as data compaction. For more information, see Understanding compute cost and the Snowflake service consumption table.

For Iceberg tables that use Snowflake storage, Snowflake manages orphan file cleanup at no cost to you.

Cloud storage costs

Your cloud storage provider charges you for the requests that Snowflake makes against your bucket during each run, such as list, get, tag, and delete requests. Because versioning is required, deleted files remain in your bucket as noncurrent object versions, which continue to incur storage charges until you remove them.

To reduce these charges, consider adding an S3 Lifecycle rule to your bucket that permanently deletes noncurrent object versions. Snowflake recommends setting Days after objects become noncurrent to 14 days. A lower value reduces storage costs, but also shortens the window in which Snowflake Support can help you recover a deleted file.