Every CNC program has to end somewhere, and two M-codes do the job: M02 and M30. Beginners are usually told that M30 is “end and rewind” and M02 is “end”, which is true but not very helpful on a machine with no tape to rewind. The real differences are about what happens next: where the program pointer goes, whether the part counter ticks, and which modal states are reset. Understanding them takes ten minutes and saves confusion at every shift change.
Where the names come from
Early numerically controlled machines read their programs from punched tape, a long strip of paper with holes encoding each character. At the end of a job the reader sat at the end of the tape. To run the next part, the tape had to be wound back to the beginning. M30 meant “end of program, rewind the tape”. M02 meant “end of program”, leaving the tape where it was, which suited jobs where the operator would rewind manually or load a different tape.
The history of numerical control explains why so many codes carry habits from that era. The tape is long gone, but M30 still does the modern equivalent of a rewind: it sends the program pointer back to the first line, so the next press of cycle start runs the program from the top.
What M30 does on a modern control
On a typical modern machining center or lathe, M30 does several things at once:
- Stops the program.
- Stops the spindle and turns off coolant, on most controls.
- Resets a set of modal states to their defaults, depending on the control and its parameters.
- Returns the program pointer to the start of the program.
- Increments the part counter, on most machines that have one.
The LinuxCNC M2 and M30 reference lists the modal resets on that control, including returning to the default work offset, plane, absolute distance mode, feed mode, and turning off the spindle and coolant. Fanuc-style controls do similar resets, governed by parameters, so the exact list varies from machine to machine.
What M02 does on a modern control
On older controls, M02 ends the program and leaves the program pointer at the end. The operator has to reset the control or select the program again before running the next part. On many modern controls, M02 and M30 have been made nearly identical, and a parameter decides whether M02 also rewinds. On LinuxCNC, the two are documented together with the same behaviour, apart from M30’s pallet shuttle function on machines that have one.
That makes M02 a code that is easy to read and rarely worth writing. If it appears in an old program and the machine handles it, there is no harm. If you are writing new programs, M30 is the safer default because its behaviour is more consistent across controls.
M30 vs M02 vs M99 vs M00 side by side
| Code | Ends the program | Returns to the top | Part counter | Typical use |
|---|---|---|---|---|
| M30 | Yes | Yes | Usually increments | End of a main program |
| M02 | Yes | Older controls: no; modern: often yes by parameter | Sometimes | Legacy programs, specific machine standards |
| M99 | No, in a main program it loops | Yes, without stopping | Usually not | End of a subprogram, or a continuous loop |
| M00 | No, it pauses | No | No | Program stop for inspection or a manual step |
| M01 | No, it pauses only if optional stop is on | No | No | Optional check points |
The last two are not program ends at all, but they are often confused with M02 and M30 by beginners. The difference between the two stop codes is covered in M00 vs M01 program stop and optional stop.
Why M30 matters for production
In production, M30 is more than an end code. Three shop systems often depend on it.
Part counters. Most controls count parts on M30. A program that ends with M02, or loops with M99, may leave the counter at zero, which throws off the shift report and any tool life management tied to the count.
Bar feeders and automation. Lathes with bar feeders and cells with robots often use the program end signal to trigger the next action. Many are set up to expect M30, and some use M99 for continuous running with a counter check. Changing the end code can quietly change how the automation behaves.
Shift handover. An operator coming on shift expects to press cycle start and run the next part. With M30, the program is already at the top. With M02 on an older control, it is not, and the first press of cycle start does nothing, or worse, the operator restarts from the wrong place.
M30 inside a subprogram
A subprogram must end with M99, which returns control to the line after the call in the main program. If a subprogram ends with M30 instead, the whole program ends at that point, and the rest of the main program never runs. It is a common mistake when a programmer copies a working main program and turns it into a subprogram without changing the last line. The subprogram structure is covered in M98 and M99 subprogram Fanuc example.
Looping instead of ending
Some programs should not end at all. A spindle warm-up, a bar feeder job that runs until the bar is used up, or a demonstration loop all want the program to restart without stopping. That is done with M99 at the end of the main program, which jumps back to the top and keeps going. The technique, and its safety considerations, are covered in how to loop a G-code program with M99. The key point is that M99 does not stop the spindle or reset modal states the way M30 does, so the program has to be written to run correctly from the top on every pass.
When M30 does not seem to work
A machine that reaches M30 and appears stuck is a familiar puzzle. Typical causes are a door interlock, an automation handshake waiting for a signal, or a parameter that requires a reset after M30. If you have met that situation, CNC machine stuck on M30 and won’t rewind walks through the usual checks.
What a good program ending looks like
A clean ending does a little more than just M30. A typical milling program ending looks like this:
G80
M09
M05
G91 G28 Z0
G28 Y0
G90
M30
That cancels any canned cycle, turns off coolant and spindle explicitly, retracts Z to home, brings the table forward for unloading, restores absolute mode, and ends with M30. Some shops prefer G53 moves to G28 for the retract, and the details vary with the machine. The principle is to leave the machine in a known, safe state before M30 runs, just as a safety line puts it in a known state at the start, rather than relying on M30’s resets alone. The modal vs non-modal G-codes post explains why leaving states explicit helps the next program.
Program end on a lathe
Lathes use the same end codes, with a few extra habits. Before M30 a lathe program usually sends the turret to a safe index position away from the chuck, stops the spindle, and on machines with a parts catcher or bar feeder, triggers those devices first. Many lathe programs also cancel constant surface speed with G97 and set a spindle speed limit before the end, so the next program does not start with a leftover surface speed command that could spin the chuck unexpectedly fast. M30 then rewinds as usual. On a bar fed lathe running continuously, M99 may replace M30, with a counter check that stops the machine when the batch is complete.
On a Swiss-type or multi-channel lathe, each channel has its own program and its own end, and the channels usually synchronise with wait codes before the final M30 so neither ends while the other is still cutting. The principle does not change: leave each channel safe, then end.
The percent signs
On many controls, a program file starts and ends with a percent sign on its own line. That is another tape habit: the percent sign marked the start and end of the data on the tape. It is not a substitute for M30. The percent sign ends the file; M30 ends the program. Both are usually needed when loading programs from a computer.
Common mistakes with program end codes
- Ending a subprogram with M30, so the main program never finishes.
- Using M02 on a machine whose part counter only counts M30.
- Relying on M30 to reset modal states that the next program assumes.
- Switching from M30 to M99 for a loop without checking that the program runs safely from the top every time.
- Confusing M01 optional stop with an end of program.
Practising the end group
The end and stop codes are small but they come up in every program, on every exam, and in every shop conversation about a machine that “won’t start”. Drill M00, M01, M02, M30 and M99 together until each one’s effect is instant, then look at the last ten lines of real programs and say what each line does. The G-code practice page includes these codes in its drill sets.
Bottom line: M30 ends and rewinds
M30 ends the program, resets the control, returns to the first line and usually counts a part. M02 ends the program and, on older controls, stays at the end. Modern controls often treat them alike, but M30 is the safer and more common choice. Use M99 to end subprograms or loop programs, and leave the machine in a known state before M30 runs.
Frequently asked questions
What is the difference between M30 and M02?
Both end the program. M30 ends it and returns the control to the start of the program, ready for the next cycle start, and on many machines it also increments the part counter. M02 ends the program without that rewind on older controls. On many modern controls the two behave almost identically, depending on parameters.
Why does M30 say rewind?
Because programs used to be read from punched paper tape. At the end of a job the tape had to be rewound to the start before the next part. M30 triggered that rewind, while M02 simply stopped. The tape is gone, but M30 still sends the program pointer back to the first line.
Should I end my programs with M30 or M02?
Use M30 on almost every modern machining center and lathe. It resets the program to the top so the operator can press cycle start for the next part, and most part counters and bar feeders key off it. Use M02 only where your machine documentation or shop standard calls for it.
What is the best way to remember the program end codes?
Link M30 to the next part and M02 to just stopping. Then drill the whole M-code end group together, M00, M01, M02 and M30, until each meaning is instant. A free app like G-Code Sprint runs timed rounds on those codes and repeats the ones you get wrong.
When is M30 not the right way to end a program?
When the program should loop without stopping, such as a warm-up cycle or a bar feeder run that repeats, where M99 at the end of the main program is used instead. And inside a subprogram, which must end with M99 to return to the caller; an M30 there ends the whole job early.