Search code examples
google-cloud-platformgoogle-cloud-tasks

Google Cloud App Engine Task Purge Queue using Node.js


I'm working on a project that uses GCP/GAE. I have a queue of tasks that is executed. In some cases I need to purge the queue contents. I have searched for API docs on how to do this, but have only found legacy outdated docs and they're only for python. I need a code example of how to purge a task queue by name using Node.js. I also did not find anything on Stackoverflow. I already have an instance of CloudTasksClient() that is used to listTasks() and it works just fine.

Is there a method for the client that will purgeQueue(<queueName>)? Does anybody know how to do this?


Solution

  • Here's how to purge a queue using Node.js

    const {CloudTasksClient} = require('@google-cloud/tasks');
    const client = new CloudTasksClient();
    
    async function main () {
      const project = "PROJECT-ID";
      const region = "REGION"
      const queue = "QUEUE"
    
      const formattedName = client.queuePath(project, region, queue);
    
      client.purgeQueue({name: formattedName})
      .then(responses => {
        const response = responses[0];
        console.log(`Purged ${queue}`);
      })
      .catch(err => {
        console.error(err);
      });
    }
    main();
    

    The proper syntax of the method should be purgeQueue(request) or purgeQueue(request,callback) where request is an object with a String property of "name".

    Though the example is based from the older version, I was able to run this on client library version 2.1.0.

    Additional Reference: https://googleapis.dev/nodejs/tasks/1.6.1/v2beta2.CloudTasksClient.html#purgeQueue