Solving Data Loss in Google Colab with Google Drive Mounting
Stop losing progress in Google Colab. Learn how to mount Google Drive for persistent storage of datasets and model checkpoints, including I/O optimization tips.
14 Oct 2025, 22:08 UTC

The Ephemeral Storage Problem
Google Colab provides powerful GPU and TPU resources, but the virtual machine (VM) assigned to your notebook is ephemeral. Any data saved to the default /content directory is wiped the moment the runtime is recycled or disconnects. For machine learning engineers, this means losing hours of training progress if a model checkpoint isn't saved externally.
The most direct solution is mounting Google Drive. This process maps your cloud storage as a local directory within the Colab VM, allowing you to treat your Drive as a persistent hard drive for datasets, weights, and logs.
Implementing the Drive Mount
Colab provides a dedicated Python module, google.colab.drive, to handle the authentication and mounting process. When you run the mount command, Colab initiates an OAuth flow, requesting permission to access your files.
from google.colab import drive
drive.mount('/content/drive')
Once authenticated, your files are accessible under the path /content/drive/MyDrive. Because the VM is reset on every new session, you must run this mount code at the start of every notebook. To avoid redundant prompts during a single session, you can wrap the mount in a conditional check:
import os
if not os.path.ismount('/content/drive'):
from google.colab import drive
drive.mount('/content/drive')
Verifying the Connection
To confirm the mount was successful and you have the correct permissions, run a shell command to list the root of your Drive:
!ls /content/drive/MyDrive
If the command returns your Drive folders without a "Permission denied" error, the filesystem is ready for read/write operations.
Worked Example: Persistent Model Checkpointing
Saving a model's state is the most common use case for persistent storage. In this example, we use PyTorch to save a model's state_dict to Drive, ensuring that if the runtime crashes, we can resume from the last saved epoch.
import torch
import torch.nn as nn
import os
from google.colab import drive
# 1. Mount and setup directory
drive.mount('/content/drive')
checkpoint_path = '/content/drive/MyDrive/colab_checkpoints/'
os.makedirs(checkpoint_path, exist_ok=True)
# 2. Define a simple model
model = nn.Linear(10, 1)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
# 3. Save the state (simulating the end of an epoch)
torch.save({
'epoch': 1,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
}, os.path.join(checkpoint_path, 'checkpoint.pth'))
print(f"Model saved to {checkpoint_path}")
To resume training after a runtime reset, re-run the mount cell and load the file:
checkpoint = torch.load(os.path.join(checkpoint_path, 'checkpoint.pth'))
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
print("Resumed from epoch:", checkpoint['epoch'])
Performance Trade-offs and Constraints
While convenient, mounting Google Drive introduces specific bottlenecks that can impact training performance.
I/O Latency
The mount is a network-based filesystem. Reading thousands of small files (like an image dataset) directly from /content/drive/MyDrive is significantly slower than reading from the local VM SSD. This can lead to "GPU starvation," where the GPU sits idle waiting for data to load.
Optimization: For large datasets, zip your data on Drive, copy the zip file to /content (local SSD), and unzip it there at the start of the session.
Quota and Limits
- Daily Quotas: Google Drive enforces API quotas. Extremely frequent read/write operations (e.g., saving a checkpoint every few seconds) may trigger a quota limit, causing the mount to become unresponsive until the reset (typically midnight UTC).
- File Size: Free accounts are subject to general storage limits. Ensure your checkpoints do not exceed your available Drive space.
Actionable Summary
- Organize: Create a dedicated project folder in Drive (e.g.,
/MyDrive/project_name/) to avoid cluttering your root directory. - Minimize Writes: Save checkpoints every few epochs rather than every batch to avoid I/O bottlenecks and quota issues.
- Local Cache: Copy large datasets from Drive to
/contentat the start of the script for faster training. - Verify: Use
!du -sh /content/drive/MyDrive/project_nameto monitor the disk space used by your model weights and logs.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.