Check the end gcode in your slicer. And maybe open an actual sliced gcode file to see what it's doing between the last print move and first line of your end gcode. The print SHOULD retract and then go into whatever movements you put in your end gcode.