Google Colab
runtime disconnection
data recovery
session restore
machine learning tools

Google Colab Can we restore all the data even after the runtime disconnects?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Google Colab runtimes are temporary, so disconnects can erase local files and in-memory variables. Recovery is only possible for artifacts you already saved outside runtime storage. The right question is not whether disconnected state can be magically restored, but whether your notebook workflow is built for restart resilience.

What You Lose After Disconnect

When Colab runtime resets, these are typically lost:

  • Python variables in memory.
  • Files written only under /content.
  • Temporary package installs done interactively.

What usually persists:

  • Notebook source in Drive.
  • Files saved to mounted Drive.
  • External datasets in cloud storage.

This difference defines your recovery strategy.

Persist Important Artifacts to Drive

Mount Drive early and write outputs there as part of normal flow, not only at the end.

python
1from google.colab import drive
2from pathlib import Path
3
4drive.mount('/content/drive')
5
6out_dir = Path('/content/drive/MyDrive/colab_runs/exp_01')
7out_dir.mkdir(parents=True, exist_ok=True)
8
9(out_dir / 'status.txt').write_text('run started\n', encoding='utf-8')
10print('saved to', out_dir)

If outputs never leave /content, disconnect usually means permanent loss.

Save Checkpoints During Long Training

For machine learning jobs, periodic checkpoints are mandatory.

python
1import os
2import tensorflow as tf
3
4ckpt_dir = '/content/drive/MyDrive/colab_runs/exp_01/checkpoints'
5os.makedirs(ckpt_dir, exist_ok=True)
6
7model = tf.keras.Sequential([
8    tf.keras.layers.Input(shape=(4,)),
9    tf.keras.layers.Dense(8, activation='relu'),
10    tf.keras.layers.Dense(1)
11])
12model.compile(optimizer='adam', loss='mse')
13
14callback = tf.keras.callbacks.ModelCheckpoint(
15    filepath=os.path.join(ckpt_dir, 'weights.{epoch:02d}.h5'),
16    save_weights_only=True,
17    save_freq='epoch'
18)

Checkpointing every epoch is usually far safer than one final save.

Keep Notebook Setup Reproducible

After reconnect, environment recreation should be one cell, not manual guesswork.

python
1!pip install -q pandas==2.2.2 numpy==1.26.4
2
3import random
4import numpy as np
5
6seed = 42
7random.seed(seed)
8np.random.seed(seed)

Pinned dependencies plus explicit seeds make resumed runs predictable.

Save Run Metadata for Recovery

Artifacts without metadata are hard to resume correctly. Save progress state and configuration along with outputs.

python
1import json
2
3meta = {
4    'run_id': 'exp_01',
5    'last_epoch': 5,
6    'dataset_version': 'v3',
7    'notes': 'resumable checkpoint state'
8}
9
10with open('/content/drive/MyDrive/colab_runs/exp_01/meta.json', 'w') as f:
11    json.dump(meta, f, indent=2)

This prevents confusion about which checkpoint and parameters belong together.

Recovery Playbook After Runtime Reset

A reliable restart flow:

  1. Reconnect runtime.
  2. Mount Drive.
  3. Reinstall dependencies using setup cell.
  4. Reload metadata and checkpoints.
  5. Resume from last saved state.

Treat this as normal operation and document it at top of notebook.

For Large Workloads, Use External Storage

Drive works for many projects, but large datasets and frequent checkpoints can outgrow it. In those cases, use cloud object storage plus experiment tracking tools. The principle remains unchanged: state must live outside transient runtime.

Design Notebook Cells for Idempotency

Cells should be safe to rerun after disconnect:

  • Create directories with exist_ok=True.
  • Avoid duplicate side effects where possible.
  • Derive paths from run identifiers.

Idempotent cells reduce human error during recovery.

Practical Checklist Before Long Runs

Before starting a long Colab session, verify that checkpoint path exists, metadata file writes correctly, and a test artifact can be reloaded from external storage. This quick preflight catches path and permission problems early, when fixing them is still cheap.

Common Pitfalls

  • Keeping critical files only in /content.
  • Assuming notebook output cells are persistent run records.
  • Saving model only at end of long training jobs.
  • Installing dependencies manually with no reproducible setup cell.
  • Omitting metadata and then resuming from wrong checkpoint.

Summary

  • Colab runtime disconnects can erase unsaved local state.
  • Recovery depends on artifacts persisted to external storage.
  • Frequent checkpoints are essential for long-running training tasks.
  • Reproducible setup cells make reconnect workflows practical.
  • Metadata plus structured run folders turn disconnects into manageable events.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design