How do I delete individual responses in FormApp

10,855

Solution 1

Nope, not there. And no external API to fill the gap.

So here's a crazy idea.

You could write a script that gets all the responses, calls deleteAllResponses(), then writes back all but the one(s) you want deleted. You'd then have summary info that reflects the responses you care about, but you'd have lost the submission dates (...which you could add as non-form data in a spreadsheet), and submitter UserID (in Apps Domains only, and again you could retain it outside the form).

Whether or not the compromises are acceptable depend on what your intended use of the form data is.

Code

This is a forms-contained script with a simple menu & UI, which will delete indicated responses. The content of deleted responses are logged.

/**
 * Adds a custom menu to the active form, containing a single menu item for
 * invoking deleteResponsesUI() specified below.
 */
function onOpen() {
  FormApp.getUi()
      .createMenu('My Menu')
      .addItem('Delete response(s)', 'deleteResponsesUI')
      .addToUi();
}

/**
 * UI for Forms function, deleteResponses().
 */
function deleteResponsesUI() {
  var ui = FormApp.getUi();
  var response = ui.prompt("Delete Response(s)",
                           "List of resonse #s to delete, separated by commas",
                           ui.ButtonSet.OK_CANCEL);
  if (response.getSelectedButton() == ui.Button.OK) {
    var deleteCSV = response.getResponseText();
    var numDeleted = deleteResponses(deleteCSV.split(/ *, */));
    ui.alert("Deleted "+numDeleted+" responses.", ui.ButtonSet.OK);
  }
}

/**
 * Deletes the indicated response(s) from the form.
 * CAVEAT: Timestamps for all remaining responses will be changed.
 * Deleted responses are logged, but cannot be recovered.
 *
 * @parameter {Number or Number[]}   Reponse(s) to be deleted, 0-indexed.
 *
 * @returns {Number}                 Number of responses that were deleted.
 */
function deleteResponses(trash) {
  if (!trash) throw new Error( "Missing parameter(s)" );
  Logger.log(JSON.stringify(trash))
  if (!Array.isArray(trash)) trash = [trash];    // If we didn't get an array, fix it

  var form = FormApp.getActiveForm();
  var responses = form.getResponses();

  // Really feels like we should ask "ARE YOU REALLY, REALLY SURE?"
  form.deleteAllResponses();

  var numDeleted = 0;
  for (var i = 0; i < responses.length; i++) {
    if ( trash.indexOf(i.toString()) !== -1 ) {
      // This response to be deleted
      Logger.log( "Deleted response: " + JSON.stringify(itemizeResponse(responses[i] )) )
      numDeleted++
    }
    else {
      // This response to be kept
      var newResponse = form.createResponse();
      var itemResponses = responses[i].getItemResponses();
      for (var j = 0; j < itemResponses.length; j++) {
        newResponse.withItemResponse(itemResponses[j]);
      }
      newResponse.submit();
    }
  }
  return numDeleted
}

/**
 * Returns item responses as a javascript object (name/value pairs).
 *
 * @param {Response}     Form Response object
 *
 * @returns              Simple object with all item responses + timestamp
 */
function itemizeResponse(response) {
  if (!response) throw new Error( "Missing parameter(s)" );

  var itemResponses = response.getItemResponses();
  var itemizedResponse = {"Timestamp":response.getTimestamp()};

  for (var j = 0; j < itemResponses.length; j++) {
    itemizedResponse[itemResponses[j].getItem().getTitle()] = itemResponses[j].getResponse();
  }
  return itemizedResponse;
}

Solution 2

It is now possible by script:

To delete a response you need the id of the response you wish to delete:

FormApp.getActiveForm().deleteResponse(responseId) 

note: you may also get the form with openById

FormApp.openById(form_id).deleteResponse(responseId) 

ref: https://developers.google.com/apps-script/reference/forms/form#deleteresponseresponseid

note: the response is permanently removed from both the summary and individual responses.

Solution 3

@james-ferreira

New Google Forms allows you to delete even individual responses from within a Google Form itself without the need of a script.
The answer that this wasn't possible by @mogsdad and @john was true until very recently.

--This is now possible on the New Google Forms--

Google announcement on the 10th of February 2016. (New Google Forms is now the default option)

Delete ALL of the responses: Delete ALL responses

Delete individual responses: Delete Individual

To delete individual responses you click on the "Responses" tab and choose "Individual". You locate the record you wish to delete and click on the trash can icon to delete that individual response.
Make a note however that the response/s will NOT be deleted from the connected to the form spreadsheet.

Solution 4

You can delete all responses from a form using deleteAllResponses(), but not individual responses. You can't even delete individual responses manually. If your form responses are directed to a spreadsheet, you use the Spreadsheet Service to select and delete individual responses there.

Share:
10,855
James Ferreira
Author by

James Ferreira

James Ferreira, author of the book, "Google Script - Enterprise Application Essentials," and owner of http://GoogleScriptExamples.com, the largest place on the web for custom Google Script libraries and code examples. James has served as the Chief Information Officer for the New Mexico Attorney General; migrated government agencies to the cloud; written free software that serves more than a half million people worldwide and published numerous technology articles including a White House brief. Circle James in Google+ to find out where in the nation he will be speaking about implementing new technology or send him a message asking him to write a custom script for your business.

Updated on August 03, 2022

Comments

  • James Ferreira
    James Ferreira almost 2 years

    Shouldn't FormResponse have a remove or delete response method? https://developers.google.com/apps-script/reference/forms/form-response

    Is it there and I'm just missing it in the docs?

    I'm talking about Responses here not Items.

  • Serge insas
    Serge insas over 9 years
    Not so crazy... I'd rather say pretty smart :-) ... +1
  • Kwestion
    Kwestion about 8 years
    -1 This question is about the FormsApp in Google Apps Script, not about editing Google Forms. Please read the question thoroughly next time.
  • marikamitsos
    marikamitsos about 8 years
    @Kwestion I am afraid both your comment and downvoting are completely out of place. Should you read the question you will realize that it is tagged with both the "google-apps-script" and "google-form" tags. I have read the question. So I would kindly ask you to really read both the question and my answer in full. Furthermore, should you want to positively contribute to the community, start now (not next time) by removing this negative vote and "Please read the question thoroughly next time."
  • Mogsdad
    Mogsdad about 8 years
    @marikamitsos The [google-form] tag is for Questions related to interacting programmatically with the Google Forms service. The question was about the programming API for Google Forms. If your answer dealt with any code solution, it would be appropriate. However, as it's purely about using the web app, it's not.
  • marikamitsos
    marikamitsos almost 8 years
    @Mogsdad I am aware of the tag's description. One should consider though that the post is an old one dating back to Dec 2 '14 and your answer on Dec 3 '14. You also state right from the start "Nope, not there. And no external API to fill the gap.". So, your code was a necessity at the time. That is why I mention: "The answer that this wasn't possible by @mogsdad and @john was true until very recently. This is now possible on the New Google Forms". Anyway. I find your approach (plural) to my post too square. Users should be informed of new developments and that was my sole intention.
  • marikamitsos
    marikamitsos over 4 years
    It has been quite a while that one can also delete individual responses. Please check my answer. It is also mentioned on the link of your answer.