M30 G-Code: Program End With Rewind Explained

M30 ends a CNC program and resets the control back to the beginning. It stops the spindle, shuts off the coolant, and moves the program cursor back to line 1 so the machine is ready to run again immediately.

Key Takeaways

  • M30 ends the CNC program and rewinds the cursor to line 1 automatically
  • It turns off the spindle, coolant, and all axis movement
  • Modal commands (work offsets, tool comp, etc.) are reset to machine defaults
  • M30 is the standard program-end code on modern CNC controls — use this unless you have a specific reason not to
  • M99 — not M30 — is used to end a subprogram
M30 – At A Glance
FunctionProgram End With Rewind
TypeNon-modal (one-shot)
Stops spindle?Yes
Stops coolant?Yes
Rewinds to start?Yes
Resets modals?Yes
Related codesM02 (end no rewind), M99 (subprogram end)

What Does M30 Do?

When the CNC control reads M30, the program is done. Here is exactly what happens in order:

  • All axis movement stops
  • The spindle stops rotating
  • Coolant shuts off
  • Modal commands reset to the machine’s default state
  • The program cursor returns to line 1

That last two points are what make M30 the standard choice over M02. When you press Cycle Start again, the program runs from the top with a clean slate — no leftover modal states from the previous run.

Why It’s Called “Program End With Rewind”

The name comes from the days when CNC programs were stored on punched paper tape. M30 told the machine to physically rewind the tape back to the beginning so it was ready to run again. On modern CNC machines, there is no tape — programs live in memory — but the name stuck.

M30 vs. M02: What Is the Difference?

Both codes end the program, but they behave differently afterward. M30 is the modern standard. M02 is mostly seen on older machines or legacy programs.

M30M02
Ends the programYesYes
Rewinds to startYesNo
Resets modal commandsYesNo (modals stay active)
Works on modern machinesYesSometimes
Common use todayStandardRarely used

M02 leaves modal commands active from the last run. If G41 cutter comp or G43 tool length offset is still on when you restart, the next run can behave unexpectedly — or alarm out on some controls. M30 clears all of that automatically, which is why it is the safer default.

Try our visualizer below to see how things are left after ending the program with either M02 or M30.

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

Some older machines can be configured to treat M02 as if it were M30. Check your machine manual if you are working with legacy programs.

How to End a CNC Program Safely With M30

M30 handles the technical reset, but you still need to make sure the machine is physically in a safe state before it fires. Do not just slap M30 at the end of your program.

Step 1 -- Return Z to a Safe Height

Rapid the Z-axis to machine home or a safe clearance height before the program ends. Use G28 Z0 for machine home, or a G53 move to a known safe position. This gets the tool away from the part so the operator can remove it safely.

Step 2 -- Cancel Coolant With M09

Turn off coolant explicitly with M09 before M30. Most controls kill coolant automatically when M30 runs, but being explicit makes the code easier to read and troubleshoot.

Step 3 -- Stop the Spindle With M05

Same idea -- M30 will stop the spindle, but calling M05 explicitly first makes the shutdown sequence clear to anyone reading the program.

A clean M30 ending looks like this:

G91 G28 Z0     (Return Z to machine home)
G28 X0 Y0      (Optional -- return X/Y to home)
M09            (Coolant off)
M05            (Spindle off)
M30            (End program, rewind to start)

COMMON MISTAKE - Ending the program with M30 while the tool is still close to the part

Why it matters: If Z is not at a safe height when the program ends, the operator has to jog the axis manually before removing the part -- adding time and risk. On some setups this can also cause the tool to drag across the part surface.

How to End a Subprogram

M30 ends a main program. It cannot be used to end a subprogram (called with M98) and return control to the main program.

The only code that ends a subprogram is M99. When the control reads M99, it jumps back 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 -- rewinds to start)

(Subprogram)
O2000
G01 Z-0.5 F10.0
G00 Z1.0
M99              (Return to main program -- NOT M30)
Diagram showing CNC program flow from main program through M98 subprogram call and M99 return to main

If you accidentally use M30 inside a subprogram, the machine will stop completely and not return to the main program. This is a common mistake when copying program endings from a main program into a subprogram.

Subprograms can call other subprograms (nesting), but most programmers keep it simple and only call subprograms from the main program to avoid confusion.

FAQS

What does M30 do on a CNC machine?

M30 ends the program, stops the spindle and coolant, resets modal commands to default, and moves the program cursor back to line 1 so the machine is ready to run again.

What is the difference between M30 and M02?

Both stop the program, but M30 rewinds to line 1 and resets modal commands. M02 does not rewind and leaves modal settings active from the previous run. M30 is the standard on modern machines.

Can I use M30 to end a subprogram?

No. Using M30 inside a subprogram stops the entire program instead of returning to the main program. Always use M99 to end a subprogram.

What happens if I forget M30?

Most CNC controls will alarm out or stop at the last line of code. Some older controls may loop or behave unpredictably. Always end your main program with M30 (or M02 on older machines).

Leave a Comment