Sub topic 14
Migrating DBFS-based code to volumes
This chapter assumes you can read Python and SQL notebooks, know the three-level namespace (catalog.schema.object), and have created a volume. It covers how to move code that reads or writes DBFS paths onto Unity Catalog volumes.
Why migrate
DBFS (the Databricks File System) is the older workspace-level storage layer. Its root and mounts sit outside Unity Catalog governance, so you cannot grant or audit access per directory. Volumes put files under the same privilege model as tables. New code should use volumes.
Step 1: Inventory the DBFS references
Search notebooks and jobs for these patterns:
dbfs:/URIs, such asdbfs:/FileStore//dbfs/paths used withopen(),pandasoros/mnt/paths, which come from legacy mountsdbutils.fs.mountcalls
Each one needs a volume path to replace it.
Step 2: Create the target volume
Pick a catalog and schema, then create a volume. Use a managed volume for new files. Use an external volume if the data must stay at an existing cloud storage location that other systems also use.
CREATE VOLUME IF NOT EXISTS main.default.landing
COMMENT 'Raw files before ingestion';
GRANT READ VOLUME, WRITE VOLUME ON VOLUME main.default.landing TO `data-engineers`;For mounts, create an external volume over the same storage location (this needs an external location configured first). Then the files do not need to be copied.
Step 3: Copy the files
src = "file:/tmp/sales.csv"
dst = "/Volumes/main/default/landing/sales.csv"
dbutils.fs.cp(src, dst)
display(dbutils.fs.ls("/Volumes/main/default/landing/"))To move files that already sit in DBFS, use the dbfs:/ path as the source. For large trees, dbutils.fs.cp(src, dst, recurse=True) copies a whole directory.
Step 4: Rewrite the paths
The change is mostly a prefix swap. A volume path is /Volumes/<catalog>/<schema>/<volume>/<path>.
| Legacy form | Volume form |
|---|---|
| `dbfs:/FileStore/sales.csv` | `/Volumes/main/default/landing/sales.csv` |
| `/dbfs/FileStore/sales.csv` | `/Volumes/main/default/landing/sales.csv` |
| `/mnt/raw/sales.csv` | `/Volumes/main/default/landing/sales.csv` |
Volume paths work with Spark, dbutils, and standard Python file APIs. So you drop the /dbfs prefix instead of replacing it with something else.
# Legacy pattern: DBFS root path
display(dbutils.fs.ls("dbfs:/FileStore/"))
# Preferred pattern: Unity Catalog volume path
display(dbutils.fs.ls("/Volumes/main/default/landing/"))path = "/Volumes/main/default/landing/sales.csv"
df = spark.read.csv(path, header=True, inferSchema=True)
df.show(5)
with open(path) as f:
print(f.readline())Step 5: Test and cut over
Run the migration in a safe order:
- Copy the data and leave the DBFS originals in place.
- Change the code to read from the volume.
- Run the job and compare row counts or checksums with the old output.
- Update downstream jobs and any scheduled runs.
- Delete the DBFS copy only after everything passes.
Common errors
- Permission denied on a volume path. The running user or job principal lacks
READ VOLUMEorWRITE VOLUME. It also needsUSE CATALOGandUSE SCHEMAon the parents. - Path not found after a prefix swap. Check the catalog, schema and volume names. A volume path has exactly three names before the file path.
- Leftover
/dbfs/prefix. It often hides inside string concatenation or config files, so search those too. - Mount still in use. Do not unmount until every job that reads
/mnt/has been updated.
Exercise
A notebook runs pd.read_csv("/dbfs/FileStore/sales.csv"). The file was copied to the volume main.default.landing. Write the updated line and name one privilege the job's user needs.
Show answer
pd.read_csv("/Volumes/main/default/landing/sales.csv"). The user needs READ VOLUME on main.default.landing, plus USE CATALOG on main and USE SCHEMA on main.default.