There is an increasing interest in the research community and the pharmaceutical industry for the development of an on-demand drug delivery system (DDS) for capsule endoscopy. Such a system is essential for site-specific drug delivery in order to treat diseases in the gastrointestinal tract in a non-invasive way. Despite different attempts to magnetically actuate DDS, two main problems remain with such systems. The first one is related to the operating distance and the second one is concerned with the miniaturization of mechanisms inside the capsule endoscope (CE). In this paper, we present an optimization of external permanent magnets to address both problems. An array of magnets is optimally designed to compensate the loss of magnetic link due to longer operating distances and miniaturization of components in the CE.