Identifying patients who are medication nonpersistent (fail to refill in a timely manner) is important for healthcare operations and research. However, consistent methods to detect nonpersistence using electronic pharmacy records are presently lacking. We developed and validated a nonpersistence algorithm for chronically used medications. Refill patterns of adult diabetes patients (n?=?14,349) prescribed cardiometabolic therapies were studied. We evaluated various grace periods (30-300 days) to identify medication nonpersistence, which is defined as a gap between refills that exceeds a threshold equal to the last days’ supply dispensed plus a grace period plus days of stockpiled medication. Since data on medication stockpiles are typically unavailable for ongoing users, we compared nonpersistence to rates calculated using algorithms that ignored stockpiles. When using grace periods equal to or greater than the number of days’ supply dispensed (i.e., at least 100 days), this novel algorithm for medication nonpersistence gave consistent results whether or not it accounted for days of stockpiled medication. The agreement (Kappa coefficients) between nonpersistence rates using algorithms with versus without stockpiling improved with longer grace periods and ranged from 0.63 (for 30 days) to 0.98 (for a 300-day grace period). Our method has utility for health care operations and research in prevalent (ongoing) and new user cohorts. The algorithm detects a subset of patients with inadequate medication-taking behavior not identified as primary nonadherent or secondary nonadherent. Healthcare systems can most comprehensively identify patients with short- or long-term medication underutilization by identifying primary nonadherence, secondary nonadherence, and nonpersistence.