Refactored delete image function