Search code examples
google-apps-scriptgoogle-tasks-apigoogle-tasks

Cannot remove task using Google Apps Script


I'm using the script.google.com editor to try to delete tasks from a list as part of a bigger project however I keep getting an error which says

API call to tasks.tasks.delete failed with error: Task not found.

Here's the function I've created to remove a task from a list:

function deleteTask(){
taskid = 'ajM2UDZIZGctaXJnOTJjZg';
tasklist = 'MDk2MzE1ODgxNDkyNDE1NTI3MDA6MDow';
try {
      // Call insert method with taskDetails and taskListId to insert Task to specified tasklist.
      Tasks.Tasks.remove(taskid, taskListId);
      // Print the Task ID of created task.
      console.log('Task with ID "%s" was deleted.', taskid);
   } catch (err) {
      // TODO (developer) - Handle exception from Tasks.move() of Task API
     console.log('Failed to move task with an error: %s', err.message);
   }
}

The funny thing is, when I use https://developers.google.com/tasks/reference/rest/v1/tasks/delete
It works
able to remove tasks from developer.google.com

Any Ideas of what I might be doing wrong? I know it might be something simple, I'm still new to javascript, it's very frustrating. Any Help would be GREATLY appreciated

I've tried using the following code to grab the parent list ID and taskID. This seems to be working fine

const taskLists = Tasks.Tasklists.list();
    // If taskLists are available then print all tasklists.
    if (!taskLists.items) {
      console.log('No task lists found.');
      return;
    }
    // Print the tasklist title and tasklist id.
    for (let i = 0; i < taskLists.items.length; i++) {
      const taskList = taskLists.items[i];
      //console.log('Task list with title Tasks" and ID "%s" was found.', taskList.title, taskList.id);
      console.log(taskList);
    }

Solution

  • Modification points:

    • In your showing script, taskListId is not declared. So, I think that this might be the reason for your current issue.
    • The arguments of Tasks.Tasks.remove are tasklist: string and task: string, respectively. If taskid and taskListId in your showing script are the values of task ID and the task list ID, respectively, Tasks.Tasks.remove(taskid, taskListId); should be Tasks.Tasks.remove(taskListId, taskid);. I think that this might be also an issue.

    When these points are reflected in your script, it becomes as follows.

    Modified script:

    function deleteTask() {
      const taskId = '###'; // Please set your task ID.
      const taskListId = '###'; // Please set your task list ID.
      try {
        Tasks.Tasks.remove(taskListId, taskId);
        console.log('Task with ID "%s" was deleted.', taskId);
    
      } catch (err) {
        // TODO (developer) - Handle exception from Tasks.move() of Task API
        console.log('Failed to move task with an error: %s', err.message);
      }
    }
    
    • By this modification, the task of taskListId, taskId is removed.

    Note:

    • In this modification, it supposes that your task ID and task list ID are valid values. Please be careful about this.

    Reference: