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 or TypeScript SDK installed and LIM_API_KEY set, create a sandbox with a snapshot key, build, and delete the sandbox from your project directory:
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-snapshotimport 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);
}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:
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-snapshotconst 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.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:
lim gradle build . --id "$GRADLE_ID" --snapshot-key myapp-mainThe 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:
lim gradle snapshot list --prefix myapp- --limit 25
lim gradle snapshot get myapp-main
lim gradle snapshot delete myapp-mainflush 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:
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 buildsucceeds. A failed build, or a sync after the last successful build, skips the save until another build succeeds. Commands fromlim gradle runalone 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.
Reuse Gradle caches and build outputs across Gradle sandboxes with disk snapshots.
Was this guide helpful?