How to Delete All Google Glass Timeline Items

Google Glass logo

For my QSL Query for Google Glass application, I needed a function that deletes all timeline items at once, rather than deleting them one at a time. The following code is written in Go, and is inserted into main.go if you are using Google’s Go Quick Start Project as your Glassware template.

// deleteAllTimelineItems deletes all timeline items.
func deleteAllTimelineItems(r *http.Request, svc *mirror.Service) string {
  timelineItems, err := svc.Timeline.List().Do()
  if err != nil {
    return fmt.Sprintf("An error occurred: %vn", err)
  }
  for _, t := range timelineItems.Items {
    err := svc.Timeline.Delete(t.Id).Do()
    if err != nil {
      return fmt.Sprintf("An error occurred: %vn", err)
    }
  }
  return "All timeline items have been deleted."
}

This function can be called from within other code blocks, or a button can be added to index.html:

<form action="/" method="post">
  <input type="hidden" name="operation" value="deleteAllTimelineItems" />
  <button class="btn" type="submit">Delete All Timeline Items</button>
  <input type="hidden" name="contentType" value="image/png" />
</form>

If it is going to be called with this button, the name of the operation must be added to the operations variable in main.go – it’s the last line before the closing curly brace.

// Map of operations to functions.
var operations = map[string]func(*http.Request, *mirror.Service) string{
    "insertSubscription":       insertSubscription,
    "deleteSubscription":       deleteSubscription,
    "insertItem":               insertItem,
    "insertItemWithAction":     insertItemWithAction,
    "insertItemAllUsers":       insertItemAllUsers,
    "insertContact":            insertContact,
    "deleteContact":            deleteContact,
    "deleteTimelineItem":       deleteTimelineItem,
    "deleteAllTimelineItems":   deleteAllTimelineItems,
}