Organic-inorganic halide perovskite (OHP) materials, for example, CH NH PbI (MAPbI ), have attracted significant interest for applications such as solar cells, photodectors, light-emitting diodes, and lasers. Previous studies have shown that charged defects can migrate in perovskites under an electric field and/or light illumination, potentially preventing these devices from practical applications. Understanding and control of the defect generation and movement will not only lead to more stable devices but also new device concepts. Here, it is shown that the formation/annihilation of iodine vacancies (V 's) in MAPbI films, driven by electric fields and light illumination, can induce pronounced resistive switching effects. Due to a low diffusion energy barrier (≈0.17 eV), the V 's can readily drift under an electric field, and spontaneously diffuse with a concentration gradient. It is shown that the V diffusion process can be suppressed by controlling the affinity of the contact electrode material to I ions, or by light illumination. An electrical-write and optical-erase memory element is further demonstrated by coupling ion migration with electric fields and light illumination. These results provide guidance toward improved stability and performance of perovskite-based optoelectronic systems, and can lead to the development of solid-state devices that couple ionics, electronics, and optics.