google-apps-scriptgoogle-workspacegoogle-groups

AdminDirectory Delete Google Group Aliases


I am trying (and failing) to delete a Google Group Alias.

function deleteGroupAlias() {
 // 1st Option
 try{
   var groupKey = "02r0uhxc4l270gq"; 
   var resource = { aliases: [ 'testeandoabms5@mydomain.com'] }
   var group = AdminDirectory.Groups.aliases.delete(resource, groupKey)
 } catch (e){ console.log(1,e) }

// 2nd Option
  try{
   var groupKey2 = "02r0uhxc4l270gq"; 
   var resource2 = 'testeandoabms5@mydomain.com'
   var group = AdminDirectory.Groups.aliases.delete(resource2, groupKey2)
  } catch (e){ console.log(2,e) }

// 3rd Option
try {
   var groupKey3 = "02r0uhxc4l270gq"; 
   var resource3 = { alias: 'testeandoabms5@mydomain.com'}
   var group = AdminDirectory.Groups.aliases.delete(resource3, groupKey3)
} catch (e){ console.log(3,e) }

// 4th Option
try {
let parametros = {
      "groupKey": "02r0uhxc4l270gq",
      "alias": "testeandoabms5@mydomain.com"
    }
    let group = AdminDirectory.Groups.aliases.delete(parametros)
} catch (e){ console.log(4,e) }

}

All variations of this code always returns: Error

[TypeError: Cannot read property 'delete' of undefined]

Of course, I can see the Google Group and Aliases in the Google Admin Console.

Many thanks


Solution

  • You need to do minimal changes to the code. I used your 2nd option for testing. Please try the following:

    
    function deleteGroupAlias()
    {
      try
      {
        var groupKey2 = "groupID"; 
        var resource2 = 'deletealias@domain.com'
        var group = AdminDirectory.Groups.Aliases.remove(groupKey2, resource2)
      } catch (e){ console.log(2,e) }
    }
    

    I tested it myself and it is working fine. I think the problem is that you were using

    AdminDirectory.Groups.aliases.delete
    

    Instead you need to use

    AdminDirectory.Groups.Aliases.remove
    

    You also need to send the group key as the first parameter.