We have studied vortex core switching in a Permalloy disk with a cavity defect using micromagnetic simulation and analytical calculation methods. It is demonstrated that the existence of a cavity that can attract and pin the vortex core in the nanodisk plays an important role in controlling the reversal of the vortex core by coherent excitation. The diagram of the vortex core switching with different cavity radii is presented based on the calculation of the effective potential energy. It is shown that there are three cases that are dependent on the distance between the cavity and the disk centers. For a smaller distance, the reversal cannot occur without applying a second sufficiently strong field pulse with the direction opposite to the orientation of the first pulse, and the amplitude of the second field pulse that can release and reverse the polarity of the vortex core increases with increasing of the radius of the cavity. Then, for a critical distance range, the vortex core can be released from the cavity and reverse automatically. When the distance is large enough the vortex core cannot be attracted and pinned by the cavity, the reversal cannot be controlled by the cavity.