COVID-19 registration form : automatically delete responses to Google Forms using Google App Script


Are you using a  Google Form to collect user data ?

Do you need to remove responses based on the date the response was made ?

EG : a COVID-19 contact tracing form, where customers leave there contact details and you need to (automatically) remove the data older then 14 days ?

Then you can use this script.


function deleteFormResponsesCovid() 

var currentDate = new Date();//today
var olderThen = + -14*24*3600*1000; //-14 is the number of days, 24*3600*1000 is the number of milliseconds in 1 day
var form = FormApp.getActiveForm(); // get the active form
var formResponses = form.getResponses(); // get all the responses of the form
for (var i = 0; i < formResponses.length; i++) 
  { //loop through the responses
  var formResponse = formResponses[i]; // get the individual respons
  var time = formResponse.getTimestamp(); //get the timestamp of the respons
   if (time < olderThen)
    var iD = formResponse.getId(); //get the id of the respons    
    form.deleteResponse(iD); //delete the responses that are older then number of given days