The microscopic doping mechanism behind the superconductor-to-insulator transition of a thin film of YBa2Cu3O7 was recently identified as due to the migration of O atoms from the CuO chains of the film. Here we employ density-functional theory calculations to study the evolution of the electronic structure of a slab of YBa2 Cu3 O7 in presence of oxygen vacancies under the influence of an external electric field. We find that under massive electric fields isolated O atoms are pulled out of the surface consisting of CuO chains. As vacancies accumulate at the surface, a configuration with vacancies located in the chains inside the slab becomes energetically preferred thus providing a driving force for O migration towards the surface. Regardless of the defect configuration studied, the electric field is always fully screened near the surface thus negligibly affecting diffusion barriers across the film.