M30 CNC Code Explained: An Easy Intro for Beginners [Program End With Rewind]

What does the M30 CNC code do?

The M30 code ends the program and tells it to go back to the start of the program. 

This is sometimes called program end with rewind.

The rewind has to do with the way CNC program used to be read. 

They were on tapes or cards. Using M30 would tell the machine to rewind so that the machine was ready to run the program again.

The M30 code turns off the following machine functions:

  • The movement of all axes
  • Spindle rotation
  • Coolant

The other code that can be used to end a program is M02. The M02 code ends the program but does not rewind the program back to the start.

Learn CNC Programming – It’s Easier Than You Think!

Learning G Code doesn’t have to be difficult…

If you know what to focus on.

Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.

Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!

Make Learning G Code Easy

What is the difference between M02 and M30

The main function of both the M02 and M30 commands are basically the same, they stop the program.

However, there are a few differences to keep in mind.

Here is a list with the main differences between the two codes.



Program end, no rewind

Program end with rewind

Primarily for older machines

Works on older and new machines

Stops the program and leaves it at the current location

Stops the program and rewinds it back to the start

Modal commands remain active

Resets the machine to the default modes

M02 is not used often anymore. M30 is the main code used to end a program.

M02 was used for a couple of reasons back when programs were on tape or cards.

Some machines were known to have issues rewinding the program and might destroy the program tape. In other instances, the program would be taped together as a loop which meant it didn’t need to be rewound. Continuing on from the end would loop back to the start of the program.

It is worth checking your machine manual to understand how your machine will read a M02 code. Some machines can even be set to read an M02 code as if it were an M30 code. Check your manual.

How to end a subprogram

M02 and M30 can be used to end your main program, but how do you end a subprogram?

The only way to end a local subprogram, subprogram, or macro is by using the M99 command.

The M99 code tells the machine to return to the program where the subprogram was called up. Usually this means returning to the main program. 

You can run subprograms within subprograms. This is called nesting.

Nesting can get tricky to read, so the majority of programs will only run subprograms from the main program to avoid confusion.

If the programmer ends the sub-program using M30 instead of M99, the machine will not return to the main program. 

M99 must be used to tell the CNC machine to return to the line after the subprogram command (M98) was used.

Best practices for using the M30 code

You should always take safety steps before stopping the program with the M30 code.

Always check where the spindle is before finishing the program. This means it is in a safe place where it will not crash or cause an accident.

For CNC mills this can mean rapid traveling the spindle to a safe Z-value before using the M30 code.

It is also a good practice to turn off the coolant using the M09 code before stopping the program.

Want to learn more about CNC G Code?

Leave a Comment