Managing Persistent Storage in Google Colab via Google Drive Mounting
Learn how to use drive.mount() in Google Colab to prevent data loss between sessions, including performance optimizations for large datasets and API limit avoidance.
18 Mar 2026, 04:47 UTC

Solving the Ephemeral Disk Problem
Google Colab runtimes are ephemeral. When a session expires or the runtime is disconnected, any data stored in the local /content directory is permanently deleted. To avoid re-uploading datasets or losing trained model weights, you must map a persistent storage layer to the virtual machine.
The most direct solution is mounting Google Drive. This creates a virtual file system bridge, allowing the Colab VM to treat your cloud storage as a local directory. The primary takeaway is that while this solves persistence, it introduces network latency; it is ideal for saving checkpoints and loading datasets, but inefficient for high-frequency I/O operations like reading thousands of small image files individually.
Implementing the Mount Mechanism
Mounting requires the google.colab library and an OAuth2 authentication flow. When you execute the mount command, Colab requests a temporary access token to interact with the Google Drive API on your behalf.
Configuration Example
Run the following code in a Colab cell to establish the connection. You will be prompted to authorize access via a pop-up window or a provided link.
from google.colab import drive
import os
# Define the mount point
mount_path = "/content/drive"
# Execute the mount process
drive.mount(mount_path)
# Define a path to a specific project folder in your Drive
# 'MyDrive' is the root folder of your Google Drive
project_folder = "/content/drive/MyDrive/ml_project"
# Ensure the directory exists to avoid FileNotFoundError
if not os.path.exists(project_folder):
os.makedirs(project_folder)
print(f"Created directory: {project_folder}")
else:
print(f"Directory already exists: {project_folder}")
Verifying the Connection
To confirm the mount was successful and the VM can see your files, use the shell ls command. Run this in a new cell:
!ls /content/drive/MyDrive
Expected Result: A list of your Google Drive folders and files should appear in the output. If the list is empty or you receive a "No such file or directory" error, the authentication step failed or the mount path was typed incorrectly.
Performance Trade-offs and Limitations
Because the mounted drive is a network-attached storage (NAS) system rather than a local SSD, it behaves differently than the standard /content disk.
| Metric | Local VM Disk (/content) | Mounted Drive (/content/drive) |
|---|---|---|
| Persistence | Temporary (Deleted on reset) | Permanent |
| Read/Write Speed | High (Local SSD) | Low (Network Latency) |
| API Constraints | None | Subject to Drive API rate limits |
Common Engineering Mistakes
- Small File Iteration: Attempting to load a dataset of thousands of individual JPEG files directly from
/content/driveduring training. This often triggers API rate limits or causes the training loop to stall due to I/O wait times. - Hardcoding Paths: Using absolute paths that only exist in one user's Drive, making the notebook non-shareable. Use variables for the base path to allow other users to modify the root directory easily.
- Assuming Session Persistence: Assuming the drive remains mounted across different sessions. You must run the
drive.mount()cell every time you start a new runtime.
Optimizing Data Access
To bypass the latency of the mounted drive, use a "Stage and Process" workflow. Instead of reading files one-by-one from Drive, copy a compressed archive (like a .zip or .tar.gz) from Drive to the local VM disk, extract it there, and then perform your computations.
Example Workflow:
- Mount Drive.
- Copy archive:
!cp /content/drive/MyDrive/data.zip /content/data.zip - Extract locally:
!unzip /content/data.zip -d /content/dataset - Point your data loader to
/content/dataset.
Rollback and Cleanup
If you need to disconnect the drive to revoke access or clear the mount point without restarting the runtime, use the following command:
from google.colab import drive
drive.flush_and_unmount()
This ensures all pending writes are committed to the cloud before the connection is severed.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.