Biological nanostructures change their shape and function in response to external stimuli, and significant efforts have been made to design artificial biomimicking devices operating on similar principles. In this work we demonstrate a programmable nanofluidic switch, driven by elastocapillarity, and based on nanochannels built from layered two-dimensional nanomaterials possessing atomically smooth surfaces and exceptional mechanical properties. We explore operational modes of the nanoswitch and develop a theoretical framework to explain the phenomenon. By predicting the switching-reversibility phase diagram - based on material, interfacial and wetting properties, as well as the geometry of the nanofluidic circuit - we rationally design switchable nano-capsules capable of enclosing zeptoliter volumes of liquid, as small as the volumes enclosed in viruses. The nanoswitch will find useful application as an active element in integrated nanofluidic circuitry and could be used to explore nanoconfined chemistry and biochemistry, or be incorporated into shape-programmable materials.