I have this code, and I'm not getting why the getThreads property is not being read.

Is getThreads() deprecated or so?

function cleanUp() {  
var delayDays = 2 // Enter # of days before messages are moved to trash   
var maxDate = new Date(); 
var label = GmailApp.getUserLabelByName("2dehands");  
var threads = label.getThreads() || [];  
for (var i = 0; i < threads.length; i++) {  
  if (threads[i].getLastMessageDate()<maxDate)

TypeError: Cannot read property 'getThreads' of null
cleanUp @ Code.gs:6

1 Answer 1


The error means that the value/object referenced by label is null. Check if there is a label named 2dehands in the user Gmail mailbox.

  • there is, but as a sub-label named cleanup.. I thought it queried every label, also sublabels.
    – Blckpstv
    Commented Feb 6, 2021 at 22:36
  • The format for "sub-labels" is Parent Label/Sub Label Commented Feb 6, 2021 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.