In this paper, the problem of rapid probe engagement and withdrawal in atomic force microscopy (AFM) is addressed. Probe engagement to and withdrawal from the sample, respectively, are fundamental steps in all AFM operations, ranging from imaging to nanomanipulation. However, due to the highly nonlinear force-distance relation and the rapid transition between the attractive and the repulsive force dominance, a quick "snap-in" of the probe and excessively large repulsive force during the engagement, and a large adhesive force during the withdrawal are induced, resulting in sample deformation and damage, and measurement errors. Such adverse effects become more severe when the engagement and withdrawal is at high speeds, and the sample is soft (such as the live biological samples). Rapid engagement and withdrawal is needed to achieve high-speed AFM operations, particularly, to capture and interrogate dynamic evolutions of the sample. We propose a learning-based online optimization technique to minimize the probe-sample interaction force in high-speed engagement and withdrawal. Specifically, the desired force and probe position trajectory profile is online designed by using the optimal trajectory design technique, and tracked by using iterative learning control technique. Then the designed force-trajectory profile is online optimized to minimize the engagement force and the adhesive force. The proposed rapid engagement and withdrawal technique is illustrated through experimental implementation on a Polydimethylsiloxane (PDMS) sample.