---
title: "Disk snapshots"
description: "Reuse Gradle caches and build outputs across Gradle sandboxes with disk snapshots."
canonical_url: "https://docs.limrun.com/docs/android/snapshots"
markdown_url: "https://docs.limrun.com/docs/android/snapshots.md"
last_updated: "2026-11-26"
x_farming_labs_generated_preamble: true
---

# Disk snapshots
URL: /docs/android/snapshots
LLM index: /llms.txt
Description: Reuse Gradle caches and build outputs across Gradle sandboxes with disk snapshots.

# Disk snapshots

A disk snapshot carries a Gradle sandbox's build state into your next sandbox, so the next build skips dependency downloads and reuses earlier outputs. A snapshot holds the synced project, the Gradle caches and wrapper distributions, ccache, and Android SDK packages the build installed. Snapshots belong to your organization.

Snapshots are opt-in. A sandbox uses one only when you give it a snapshot key.

## Save and reuse a disk snapshot

With the [CLI](/docs/quickstart) or [TypeScript SDK](/docs/reference/sdk#install) installed and `LIM_API_KEY` set, create a sandbox with a snapshot key, build, and delete the sandbox from your project directory:

<CodeGroup labels={["CLI","TypeScript"]}>
```bash
GRADLE_ID=$(lim gradle create --snapshot-key myapp-main --json | jq -r .metadata.id)
lim gradle build . --id "$GRADLE_ID"
lim gradle delete "$GRADLE_ID" --wait-snapshot
```

```ts
import Limrun from '@limrun/api';

const lim = new Limrun({ apiKey: process.env['LIM_API_KEY'] });
const instance = await lim.gradleInstances.create({
  wait: true,
  spec: { snapshot: { key: 'myapp-main' } },
});

try {
  const { snapshot, gone } = await lim.gradleInstances.followSnapshot(instance.metadata.id);
  if (gone || snapshot.restore.phase === 'failed') {
    throw new Error(snapshot.restore.message ?? 'Disk snapshot restore did not complete');
  }

  const gradle = await lim.gradleInstances.createClient({ instance });
  await gradle.sync('.');
  const { exitCode } = await gradle.gradlebuild({ tasks: ['assembleDebug'] });
  if (exitCode !== 0) throw new Error(`Build exited with ${exitCode}`);
} finally {
  await lim.gradleInstances.delete(instance.metadata.id);
}
```
</CodeGroup>

The first run builds from scratch. Deleting the sandbox after a successful build saves its state under `myapp-main`. The next run restores that snapshot before it builds. Each save replaces the previous snapshot under the key.

`--wait-snapshot` waits for the save and prints its result. Without it, deletion returns at once and the save continues in the background. In CI, run the delete in a cleanup step that runs even when the build fails.

In TypeScript, `followSnapshot(id)` waits for the restore, as the example does. To follow the save, call `followSnapshot(id, { side: 'save' })` and wait for its `onOpen` callback before deleting. A save phase of `published` means the snapshot was saved; `skipped` means nothing was saved.

A key uses letters, digits, and `. _ - + / : @ #`, up to 248 characters. Keys can't start with `users/` or `shared/`.

## Restore from another branch

Use `--snapshot-key` for the key to save under and `--snapshot-restore-keys` for an ordered list of keys to restore from. For example, restore a pull request's previous snapshot, fall back to `main`, and save under the pull request's key:

<CodeGroup labels={["CLI","TypeScript"]}>
```bash
GRADLE_ID=$(lim gradle create \
  --snapshot-key myapp-pr51 \
  --snapshot-restore-keys "myapp-pr51,myapp-main" \
  --json | jq -r .metadata.id)
lim gradle build . --id "$GRADLE_ID"
lim gradle delete "$GRADLE_ID" --wait-snapshot
```

```ts
const instance = await lim.gradleInstances.create({
  wait: true,
  spec: {
    snapshot: {
      key: 'myapp-pr51',
      restoreKeys: ['myapp-pr51', 'myapp-main'],
    },
  },
});
// Then wait for the restore, build, and delete as in the first example.
```
</CodeGroup>

For each restore key, Limrun tries an exact match, then the newest snapshot whose key starts with that text. It moves to the next restore key only when neither matches. With no match, the sandbox starts cold and still saves after a successful build.

Without `--snapshot-restore-keys`, the sandbox restores from its `--snapshot-key`. When you pass a restore list, include that key yourself if you want it tried first. To restore without saving, pass only `--snapshot-restore-keys`.

For concurrent CI jobs, give each job its own save key so they don't overwrite each other.

## Add a snapshot key to a running sandbox

A sandbox created without a key can still save one. Pass `--snapshot-key` to `lim gradle build` or `lim gradle sync`, or call `bindSnapshotKey(id, key)` in TypeScript:

```bash
lim gradle build . --id "$GRADLE_ID" --snapshot-key myapp-main
```

The key only sets where the sandbox saves when it is deleted. It doesn't restore anything; restores happen when the sandbox is created. A sandbox saves under one key: repeating the same key is accepted, and a different key returns an error.

## Inspect and delete saved snapshots

List, inspect, and delete snapshots by exact key:

```bash
lim gradle snapshot list --prefix myapp- --limit 25
lim gradle snapshot get myapp-main
lim gradle snapshot delete myapp-main
```

`flush` is an alias for `delete`. Deleting a snapshot doesn't affect running sandboxes; they can save the key again when they're deleted. When a list has another page, it returns `nextOffset`; pass it as `--offset`.

In TypeScript, pass your organization ID:

```ts
const organizationId = 'org_...';
const page = await lim.gradleSnapshots.list(organizationId, { prefix: 'myapp-', limit: 25 });
const snapshot = await lim.gradleSnapshots.get(organizationId, 'myapp-main');
await lim.gradleSnapshots.delete(organizationId, snapshot.key);
```

Listing and inspecting require Gradle read access; deleting requires Gradle control. A missing snapshot returns 404.

## Understand a cold build or skipped save

- A snapshot saves only after a `lim gradle build` succeeds. A failed build, or a sync after the last successful build, skips the save until another build succeeds. Commands from `lim gradle run` alone don't count.
- A restore skips snapshots saved by a sandbox image with different toolchains (NDK, CMake, JDK, or Node.js versions) and tries the next match. After an image update changes a toolchain, the first build starts cold, and its save restores normally from then on.
- Gradle's configuration cache isn't saved, because it can hold environment values and secrets. The first build after a restore configures the project again.

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
Docs-scoped sitemap: [/docs/sitemap.md](/docs/sitemap.md).
Well-known sitemap: [/.well-known/sitemap.md](/.well-known/sitemap.md).
