Skip to content

Object reference

Lock

A LockService.Lock prevents concurrent trigger runs, form submissions, or web-app requests from editing the same Sheet, Doc, or property at once. Acquire it with waitLock(ms) or tryLock(ms), flush pending Sheet writes, and release it in finally.

LockService.Lock

Start here

Get this object first

Call LockService.getScriptLock() to coordinate all runs of the script project across every user, LockService.getDocumentLock() to lock only the bound file (returns null in standalone scripts), or LockService.getUserLock() to serialize runs for the current user.

Code.gs
function checkScriptLockAvailable() {
  const lock = LockService.getScriptLock();
  if (!lock.tryLock(5000)) {
    Logger.log('Another run holds the script lock.');
    return;
  }
  try {
    Logger.log('Has lock: ' + lock.hasLock());
  } finally {
    lock.releaseLock();
  }
}

Then use it

Common actions

Choose an action, then open its example only when you need the code.

Serialize Google Sheets writes with waitLock(), flush(), and releaseLock()

Waits up to 30 seconds for the script lock, appends a row, flushes pending SpreadsheetApp changes while still holding the lock, and always releases the lock in finally.

LockService.LockSpreadsheetApp.SpreadsheetSpreadsheetApp.Sheet
Show example code
Code.gs
function appendRowSafely(values) {
  const lock = LockService.getScriptLock();
  lock.waitLock(30000);
  try {
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Log');
    if (!sheet) throw new Error('Sheet "Log" not found.');
    sheet.appendRow(values);
    SpreadsheetApp.flush();
  } finally {
    lock.releaseLock();
  }
}

Skip a trigger run when another execution holds the lock

Waits up to 2 seconds with tryLock(2000). If an earlier trigger run still holds the lock, it logs a message and returns without doing the work.

LockService.Lock
Show example code
Code.gs
function runIfScriptLockIsFree() {
  const lock = LockService.getScriptLock();
  if (!lock.tryLock(2000)) {
    Logger.log('Skipping run because another execution is active.');
    return;
  }
  try {
    Logger.log('Lock acquired for exclusive work.');
  } finally {
    lock.releaseLock();
  }
}

Guard a property update with waitLock and finally

Waits up to 10 seconds with waitLock(10000), adds one to a run counter in script properties, and always calls releaseLock() in finally. If the wait runs out, waitLock throws an error.

LockService.LockPropertiesService.Properties
Show example code
Code.gs
function incrementRunCounterWithLock() {
  const lock = LockService.getScriptLock();
  lock.waitLock(10000);
  try {
    const props = PropertiesService.getScriptProperties();
    const nextCount = Number(props.getProperty('runCount') || '0') + 1;
    props.setProperty('runCount', String(nextCount));
    Logger.log('Run count: ' + nextCount);
  } finally {
    lock.releaseLock();
  }
}

Check whether the current execution holds the lock

Logs hasLock() before acquiring the script lock, while holding it, and after releasing it.

LockService.Lock
Show example code
Code.gs
function logLockStateTransition() {
  const lock = LockService.getScriptLock();
  Logger.log('Before tryLock: ' + lock.hasLock());
  if (lock.tryLock(1000)) {
    try {
      Logger.log('Inside try block: ' + lock.hasLock());
    } finally {
      lock.releaseLock();
    }
  }
  Logger.log('After releaseLock: ' + lock.hasLock());
}

Technical reference

Exact names and signatures

Open method details when you need them.

At a glance

Service
LockService
Reference symbol
LockService.Lock
Object
Lock
Runtime
From signature data

Methods and signatures

Browse 4 methods

hasLock

From signature data Google source
  • hasLock(): boolean

releaseLock

From signature data Google source
  • releaseLock(): void

tryLock

From signature data Google source
  • tryLock(timeoutInMillis: number): boolean

waitLock

From signature data Google source
  • waitLock(timeoutInMillis: number): void

Need the whole workflow?

Return to a task guide

Task guides cover setup, permissions, expected results, and troubleshooting as well as object methods.

Browse Script utilities tasks