Volume resizing for running sandboxes

Last updated: February 26, 2026

You can resize volumes while your sandbox is running. The operation typically completes in under 1 second, making it a fast and efficient way to manage storage needs.

Important limitations

  • You can only increase the size of a volume, not decrease it

  • Auto-resize functionality is not currently available as a built-in feature

Recommended resizing strategy

To prevent running out of disk space, we recommend:

  • Check volume usage before running heavy file-consuming commands (like package installations)

  • Resize the volume if usage approaches 80%

  • Run a periodic monitoring process inside the sandbox to track usage and trigger resizing automatically

Volume monitoring code example

Here's a function that monitors a volume and automatically resizes it when usage exceeds a threshold:

async function startVolumeMonitor(
  sandbox: SandboxInstance,
  mountPath: string,
  initialSizeMB: number,
  usageThresholdPercent: number = 80,
  growthFactor: number = 2,
  pollIntervalSeconds: number = 3
) {
  let currentSizeMB = initialSizeMB;
  let resizing = false;

  const monitorCmd = `while true; do df -m ${mountPath}; sleep ${pollIntervalSeconds}; done`;

  await sandbox.process.exec({
    command: monitorCmd,
    onLog: (log) => {
      const lines = log.split("\n");
      for (const line of lines) {
        if (!line.includes(mountPath) || line.startsWith("Filesystem")) continue;

        const parts = line.trim().split(/\s+/);
        if (parts.length < 5) continue;

        const usagePercent = parseInt(parts[4]!.replace("%", ""), 10);
        if (isNaN(usagePercent)) continue;

        console.log(
          `[monitor] ${parts[2]}MB used / ${parts[1]}MB total (${usagePercent}%)`
        );

        if (usagePercent >= usageThresholdPercent && !resizing) {
          resizing = true;
          const newSize = Math.ceil(currentSizeMB * growthFactor);
          console.log(
            `[monitor] Usage ${usagePercent}% >= ${usageThresholdPercent}%, resizing ${currentSizeMB}MB -> ${newSize}MB`
          );
          VolumeInstance.update(volumeName, { size: newSize })
            .then(() => {
              currentSizeMB = newSize;
              console.log(`[monitor] Resize to ${newSize}MB complete`);
            })
            .catch((err) => {
              console.error(`[monitor] Resize failed:`, err);
            })
            .finally(() => {
              resizing = false;
            });
        }
      }
    },
  });

  return {
    getCurrentSize: () => currentSizeMB,
  };
}

This function monitors disk usage every 3 seconds and automatically doubles the volume size when usage reaches 80%. You can adjust the threshold percentage, growth factor, and polling interval based on your specific needs.