M02 G-Code: Program End Without Rewind Explained

The M02 code ends a CNC program. When the control reads M02, it stops all movement, turns off the spindle, and shuts off the coolant. Unlike M30, it does not reset back to the beginning of the program.

Key Takeaways

  • M02 ends the CNC program and stops all machine functions
  • It does NOT rewind (reset) to the start of the program — use M30 if you need that
  • M30 is the more common choice on modern machines; M02 is mostly seen on older controls
  • Always home the spindle to a safe Z position and cancel coolant before M02
  • M99 — not M02 — is used to end a subprogram
M02 – At A Glance
FunctionProgram End, No Rewind
TypeNon-modal (one-shot)
Stops spindle?Yes
Stops coolant?Yes
Resets to start?No
Related codesM30 (program end with rewind), M99 (subprogram end)

What Does M02 Do?

M02 tells the CNC machine that the program is finished. The control stops executing code. All of these things happen at the same time:

  • All axis movement stops
  • The spindle stops rotating
  • Coolant shuts off

After M02 runs, the program cursor stays at the M02 line. It does not go back to the top. On some controls, this means you have to manually rewind or re-select the program before running it again.

The Tape Rewind History

The “no rewind” name comes from older CNC machines that read programs off punched paper tape. When you ran M02, the machine stopped but didn’t rewind the tape back to the beginning. That physical tape had to be rewound by hand — or looped into a continuous roll so it would feed through again automatically. Today, tapes are gone, but the name stuck.

M02 vs. M30: What’s the Difference?

Both codes stop the program. The main difference is what happens after.

M02M30
Ends the programYesYes
Rewinds to startNoYes
Resets modal commandsNo (modals stay active)Yes (resets to defaults)
Works on newer machinesSometimesYes
Common use todayRarelyStandard

M02 leaves all modal commands — things like G41 cutter comp, G43 tool length offset, work offsets — in whatever state they were when the program ended. M30 resets the machine back to its default state. This is one reason M30 is the safer and more common choice on modern machines.

COMMON MISTAKE – Leaving modal commands on with M02

Mistake: Using M02 instead of M30 and then re-running the program without noticing modal commands are still active. Why it matters: If G41 or G43 is still on from the previous run, your next program start can behave unexpectedly — or cause a crash on machines that don’t auto-reset.

Some machines can be configured to treat M02 as if it were M30. Check your machine manual or ask your setup person if you’re unsure how your specific control handles it.

Try our program end visualizer and see for yourself the difference that using M02 vs M30 makes.

M02 vs M30 — Program End Visualizer
Idle
Running
Safe Z / M09
End Code
Final State
Program A — ends with M02
1G90 G54 G00 X0 Y0
2S1500 M03 (spindle on)
3M08 (coolant on)
4G91 G28 Z0 (Z to home)
5M09 (coolant off)
6M05 (spindle off)
7M02
Program B — ends with M30
1G90 G54 G00 X0 Y0
2S1500 M03 (spindle on)
3M08 (coolant on)
4G91 G28 Z0 (Z to home)
5M09 (coolant off)
6M05 (spindle off)
7M30
M02 — Machine State
Spindle
Coolant
Modal State
Program Cursor
Press “Run Simulation” to start.
M30 — Machine State
Spindle
Coolant
Modal State
Program Cursor
Press “Run Simulation” to start.
Key Differences After Program End
M02
M30
Program Cursor
Stays at M02 line
Rewinds to line 1
Modal Commands
Still active from last run
Reset to defaults
Ready to Re-run?
No — must rewind manually
Yes — press Cycle Start
Spindle
Off
Off
Coolant
Off
Off
Typical Use
Older / legacy machines
Standard on modern CNCs

Best Practices Before Using M02

Don't just throw M02 at the end of your program and call it done. A few safety steps first:

Return the Spindle to a Safe Position

Make sure the tool is somewhere safe before the program ends. On a mill, rapid the Z-axis up to a safe height. You can use G28 Z0 (return to machine zero in Z) or a G53 move to a known safe position. This prevents the tool from sitting close to the part when the machine stops.

G91 G28 Z0    (Incremental move — return Z to machine home)
M05           (Spindle off)
M09           (Coolant off)
M02           (End program)

Cancel Coolant With M09 First

Always cancel coolant with M09 before M02. Most controls will shut off coolant when M02 runs anyway, but it's good practice to be explicit. It also makes your code easier to read and troubleshoot.

COMMON MISTAKE - Not returning Z to a safe height before M02

Why it matters: If the program ends with the tool close to the part, the operator has to jog the axis manually before removing the part — which adds time and increases the risk of a bump or scratch.

How to End a Subprogram

M02 only ends a main program. If you're writing a subprogram (called with M98), you cannot use M02 to return control to the main program.

The only way to end a subprogram is with M99. When the control reads M99, it returns to the line after the M98 call in the main program and keeps running.

(Main Program)
O1000
G00 X1.0 Y1.0
M98 P2000        (Call subprogram O2000)
G00 X0 Y0
M30              (End main program)

(Subprogram)
O2000
G01 Z-0.5 F10.0
G00 Z1.0
M99              (Return to main program — NOT M02 or M30)
M98 Code Flow
How the CNC reads a program that contains a subprogram

If you use M02 or M30 inside a subprogram, the machine will treat the entire program as finished. It won't return to the main program. That's rarely what you want.

Subprograms can call other subprograms — this is called nesting. Most programmers keep it simple and only call subprograms from the main program to avoid confusion.

FAQs

What does M02 do on a CNC machine?

M02 ends the CNC program. It stops all axis movement, the spindle, and the coolant. The program cursor stays at the M02 line and does not return to the beginning.

What is the difference between M02 and M30?

Both end the program, but M30 rewinds back to the start and resets modal commands. M02 does not rewind and leaves modal settings in their current state. M30 is the standard choice on most modern machines.

Can I use M02 to end a subprogram?

No. To end a subprogram and return to the main program, you must use M99. Using M02 inside a subprogram will stop the entire program instead of returning to the main program.

Why is it called "no rewind"?

The name comes from the era of punched paper tape CNC programming. M02 stopped the program without rewinding the physical tape back to the start. The term stuck even though modern CNCs use memory, not tape.