Remove unused data files with vacuum
You can remove data files no longer referenced by a Delta table that are older than the retention threshold by running the
vacuum command on the table. Running
vacuum regularly is important for cost and compliance because of the following considerations:
Deleting unused data files reduces cloud storage costs.
Data files removed by
vacuummight contain records that have been modified or deleted. Permanently removing these files from cloud storage ensures these records are no longer accessible.
vacuum is not triggered automatically. The default retention threshold for data files is 7 days. To change this behavior, see Configure data retention for time travel.
Some Delta Lake features use metadata files to mark data as deleted rather than rewriting data files. You can use
REORG TABLE ... APPLY (PURGE) to commit these deletions and rewrite data files. See Purge metadata-only deletes to force data rewrite.
vacuumremoves all files from directories not managed by Delta Lake, ignoring directories beginning with
_. If you are storing additional metadata like Structured Streaming checkpoints within a Delta table directory, use a directory name such as
vacuumdeletes only data files, not log files. Log files are deleted automatically and asynchronously after checkpoint operations. The default retention period of log files is 30 days, configurable through the
delta.logRetentionDurationproperty which you set with the
ALTER TABLE SET TBLPROPERTIESSQL method. See Delta table properties reference.
The ability to time travel back to a version older than the retention period is lost after running
When disk caching is enabled, a cluster might contain data from Parquet files that have been deleted with
vacuum. Therefore, it may be possible to query the data of previous table versions whose files have been deleted. Restarting the cluster will remove the cached data. See Configure the disk cache.
Example syntax for vacuum
VACUUM eventsTable -- vacuum files not required by versions older than the default retention period VACUUM '/data/events' -- vacuum files in path-based table VACUUM delta.`/data/events/` VACUUM delta.`/data/events/` RETAIN 100 HOURS -- vacuum files not required by versions more than 100 hours old VACUUM eventsTable DRY RUN -- do dry run to get the list of files to be deleted
For Spark SQL syntax details, see VACUUM.
See the Delta Lake API documentation for Scala, Java, and Python syntax details.
Purge metadata-only deletes to force data rewrite
REORG TABLE command provides the
APPLY (PURGE) syntax to rewrite data to apply soft-deletes. Soft-deletes do not rewrite data or delete data files, but rather use metadata files to indicate that some data values have changed. See REORG TABLE.
Operations that create soft-deletes in Delta Lake include the following:
Dropping columns with column mapping enabled.
Deleting rows with deletion vectors enabled.
Any data modifications on Photon-enabled clusters when deletion vectors are enabled.
With soft-deletes enabled, old data may remain physically present in the table’s current files even after the data has been deleted or updated. To remove this data physically from the table, complete the following steps:
REORG TABLE ... APPLY (PURGE). After doing this, the old data is no longer present in the table’s current files, but it is still present in the older files that are used for time travel.
VACUUMto delete these older files.
REORG TABLE creates a new version of the table as the operation completes. All table versions in the history prior to this transaction refer to older data files. Conceptually, this is similar to the
OPTIMIZE command, where data files are rewritten even though data in the current table version stays consistent.
Data files are only deleted when the files have expired according to the
VACUUM retention period. This means that the
VACUUM must be done with a delay after the
REORG so that the older files have expired. The retention period of
VACUUM can be reduced to shorten the required waiting time, at the cost of reducing the maximum time travel history that is retained.
What size cluster does vacuum need?
To select the correct cluster size for
VACUUM, it helps to understand that the operation occurs in two phases:
The job begins by using all available executor nodes to list files in the source directory in parallel. This list is compared to all files currently referenced in the Delta transaction log to identify files to be deleted. The driver sits idle during this time.
The driver then issues deletion commands for each file to be deleted. File deletion is a driver-only operation, meaning that all operations occur in a single node while the worker nodes sit idle.
To optimize cost and performance, Databricks recommends the following, especially for long-running vacuum jobs:
Run vacuum on a cluster with auto-scaling set for 1-4 workers, where each worker has 8 cores.
Select a driver with between 8 and 32 cores. Increase the size of the driver to avoid out-of-memory (OOM) errors.
VACUUM operations are regularly deleting more than 10 thousand files or taking over 30 minutes of processing time, you might want to increase either the size of the driver or the number of workers.
If you find that the slowdown occurs while identifying files to be removed, add more worker nodes. If the slowdown occurs while delete commands are running, try increasing the size of the driver.
How frequently should you run vacuum?
Databricks recommends regularly running
VACUUM on all tables to reduce excess cloud data storage costs. The default retention threshold for vacuum is 7 days. Setting a higher threshold gives you access to a greater history for your table, but increases the number of data files stored and, as a result, incurs greater storage costs from your cloud provider.
Why can’t you vacuum a Delta table with a low retention threshold?
It is recommended that you set a retention interval to be at least 7 days,
because old snapshots and uncommitted files can still be in use by concurrent
readers or writers to the table. If
VACUUM cleans up active files,
concurrent readers can fail or, worse, tables can be corrupted when
deletes files that have not yet been committed. You must choose an interval
that is longer than the longest running concurrent transaction and the longest
period that any stream can lag behind the most recent update to the table.
Delta Lake has a safety check to prevent you from running a dangerous
VACUUM command. If you are certain that there are no operations being performed on this table that take longer than the retention interval you plan to specify, you can turn off this safety check by setting the Spark configuration property
VACUUM commits to the Delta transaction log contain audit information. You can query the audit events using
To capture audit information, enable
spark.databricks.delta.vacuum.logging.enabled. Audit logging is not enabled by default for AWS S3 tables due to the limited consistency guarantees provided by S3 with regard to multi-workspace writes. If you enable it on S3, make sure there are no workflows that involve multi-workspace writes. Failing to do so may result in data loss.