Solving the Ephemeral Storage Problem in Google Colab
Stop losing your progress in Google Colab. Learn how to use Google Drive mounting for persistent storage and how to avoid the common I/O bottlenecks when handling large datasets.
10 Nov 2025, 23:21 UTC

The Problem: The Disappearing Disk
Google Colab provides a powerful, free GPU environment, but it comes with a critical limitation: the runtime is ephemeral. Any data uploaded to the local disk (the /content/ directory) is permanently deleted the moment your session times out or the runtime is recycled. For data scientists, this means losing trained model weights, preprocessed datasets, and configuration files every time the browser tab closes.
The solution is to decouple your compute (the Colab VM) from your storage (Google Drive). By mounting Google Drive as a network file system, you create a persistent bridge that allows your code to read and write files that survive the session termination.
How Drive Mounting Works
Colab uses a FUSE-based (Filesystem in Userspace) integration. When you mount your drive, Colab maps your cloud storage to a directory in the Linux virtual machine. To the Python environment, this looks like a standard local folder, but every read and write operation is actually an API call to Google Drive.
The Mounting Process
To establish this connection, you must run a specific utility that triggers an OAuth authentication flow. This ensures the VM has permission to access your private files.
from google.colab import drive
drive.mount('/content/drive')
Execution Details: Run this in a standard code cell. You will be prompted to click a link, sign into your Google account, and paste an authorization code (or click "Allow" in the pop-up). This must be repeated every time you start a new runtime session.
Practical Example: Saving Model Weights
Saving a model to the local /content/ folder is a common mistake; the file disappears when the VM shuts down. Instead, define a path within the /content/drive/MyDrive/ directory.
Assume you are using a library like PyTorch or Keras. Here is the correct configuration for persistent saving:
import os
from google.colab import drive
# 1. Mount the drive
drive.mount('/content/drive')
# 2. Define a persistent directory
save_path = '/content/drive/MyDrive/ml_models/experiment_1/'
if not os.path.exists(save_path):
os.makedirs(save_path)
# 3. Save the model (Example using a generic save function)
# model.save(os.path.join(save_path, 'model_weights.h5'))
print(f"Model successfully saved to {save_path}")
Verifying the Result
To confirm the file exists without leaving the notebook, run a shell command in a new cell:
!ls /content/drive/MyDrive/ml_models/experiment_1/
If the filename appears in the output, the file is safely stored in your cloud storage and will be available in future sessions.
The I/O Bottleneck: A Critical Trade-off
While mounting Drive is convenient, it is not a high-performance disk. Because it operates over a network, there is significant latency for every file request. This creates a specific performance trap: the small-file problem.
| Scenario | Direct Drive Access | Local VM Access (/content/) |
|---|---|---|
| Reading one 1GB file | Efficient | Very Efficient |
| Reading 10,000 small JPEGs | Very Slow (API Overhead) | Fast (Local SSD) |
| Persistence | Permanent | Temporary |
If your dataset consists of thousands of small files, reading them directly from /content/drive/ will bottleneck your GPU, leaving it idle while the system waits for network I/O. This can also trigger Google Drive API rate limits, causing your notebook to hang.
The Optimized Workflow
To get the best of both worlds, use this strategy: store your dataset as a single .zip or .tar.gz archive on Drive, copy that archive to the local VM, and unzip it locally.
# Copy from Drive to local VM (Fast)
!cp /content/drive/MyDrive/data/dataset.zip /content/dataset.zip
# Unzip locally (Very Fast)
!unzip -q /content/dataset.zip -d /content/dataset/
Closing Summary
To ensure your work persists in Google Colab, always mount your drive and save critical outputs to /content/drive/MyDrive/. However, remember that the mount is a network bridge, not a local disk. For large-scale data loading, move compressed archives to the local runtime before processing to avoid severe I/O latency.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.