Search code examples
google-apps-scriptgoogle-cloud-platformstackdrivergoogle-cloud-logging

Google Cloud Logging API with Apps Script - delete logs - The requested URL was not found on this server


Using Apps Script, I need to delete the Stackdriver logs of a particular Google Cloud Project. This should be possible to do with Apps Script and the Cloud Logging REST API, using a DELETE request. Apps Script UrlFetchApp.fetch(url,options) can use the DELETE method. The code below is returning response code: 404. I was previously getting response code: 400, but now it's accepting the URL structure, but it just can't find the URL. If the URL needs a log ID, then I'm not sure where to get the log ID. I want to delete all logs in the project, not just a particular one.

Error Message:

The requested URL was not found on this server. 

Code:

function deleteStackDriverLogs(po) {
  var httpRz,options,url;

  /*
    po.id = GCP project ID - https://console.cloud.google.com/home/dashboard
  */

  options = {};

  options.method = "delete";
  options.muteHttpExceptions = true;
  options.headers = {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()};
  options.contentType = "application/json";

  url = 'https://logging.googleapis.com/v2/';

  options.payload = JSON.stringify({
    logName: 'projects/' + po.id + "/logs/*"
  });

  httpRz = UrlFetchApp.fetch(url,options);
  Logger.log('response code: ' + httpRz.getResponseCode());

  //Logger.log('httpRz.getAllHeaders(): ' + JSON.stringify(httpRz.getAllHeaders()))
  //Logger.log(httpRz.getContentText())

}

function testDlet() {
  deleteStackDriverLogs({"id":"project-id-your-GCP-id-here"});


}

Documentation: https://cloud.google.com/logging/docs/reference/v2/rest/v2/logs/delete

If just a URL without the payload is used, then I get response code 404 with no explanation.

I've tried many variations of the url.

url = 'https://logging.googleapis.com/v2/logName={projects/' + po.id + '}';//404
url = 'https://logging.googleapis.com/v2/logName=projects/' + po.id + '/';//404
url = 'https://logging.googleapis.com/v2/logName=projects/' + po.id;//404
url = 'https://logging.googleapis.com/v2/logName=projects/' + po.id + '/logs/*/';//400
url = 'https://logging.googleapis.com/v2/logName=projects/' + po.id + '/logs/';//404

The documentation states that the Log ID must be URL-encoded. But, I'm not sure what to use for the Log ID.


Solution

  • I believe your goal as follows.

    • You want to delete the logs of the stackdriver using Google Apps Script.

    For this, how about this answer?

    Modification points:

    • In this case, please retrieve the log name for deleting the logs.
      • You can retrieve logName using the method "logs.list" in Could Logging API v2.
    • Please use logName as the path of the endpoint.

    When above points are reflected to your script, it becomes as follows.

    Modified script:

    function deleteStackDriverLogs(po) {
    
      // --- I added below script.
      const endpoint = `https://logging.googleapis.com/v2/projects/${po.id}/logs`;
      const res = UrlFetchApp.fetch(endpoint, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}});
      const obj = JSON.parse(res.getContentText());
      const logName = obj.logNames.filter(e => e.includes("console_logs"))[0];
      /// ---
    
      var httpRz,options,url;
    
      /*
        po.id = GCP project ID - https://console.cloud.google.com/home/dashboard
      */
    
      options = {};
    
      options.method = "delete";
      options.muteHttpExceptions = true;
      options.headers = {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()};
    
      url = 'https://logging.googleapis.com/v2/' + logName;  // Modified
    
      httpRz = UrlFetchApp.fetch(url,options);
      Logger.log('response code: ' + httpRz.getResponseCode());
    
      //Logger.log('httpRz.getAllHeaders(): ' + JSON.stringify(httpRz.getAllHeaders()))
      //Logger.log(httpRz.getContentText())
    
    }
    

    Note:

    • This is a simple modification for your script. So please modify this for your actual situation.
    • This modified script supposes that you have already been able to use Could Logging API v2.
    • In this case, please use this script with V8.

    References: