Start G-code and end G-code: heat, probe, purge and park in the right order
By Sina Esfahani, OmniTechs · Published 27 November 2026
Start G-code is the block your slicer puts before the first layer; end G-code is the block after the last. A good start code sets the coordinate modes, starts the heaters, homes, waits for the bed, probes with the nozzle below oozing temperature, then heats fully, purges and resets the extruder. A good end code lifts the nozzle, moves it clear, switches the heaters and fan off and releases the motors.
Two rules prevent most problems. Take temperatures from slicer placeholders, never typed numbers, so the code follows your filament. And give your slicer heating lines it recognises: if it finds none, it adds its own before your code and breaks your order.
Build your start and end code in this order:
Start from your printer's own profile and copy it to a text editor first.
Set the modes: G90, then M83 or M82.
Start the bed with a placeholder: M140 S[first_layer_bed_temperature] in PrusaSlicer, M140 S{material_bed_temperature_layer_0} in Cura.
Home with G28, then wait for the bed with M190 before you probe.
Probe with a cooler nozzle: Prusa uses 170 °C for PLA, Bambu Lab 140 °C.
Heat to the first-layer temperature, purge, wipe, then G92 E0.
At the end: lift Z first, park, then M104 S0, M140 S0, M107 and M84 X Y E.
What you see, and what it means
Start and end G-code problems
| What you see | What it means | What to do |
|---|---|---|
| The printer heats, then waits a second time | The slicer added its own heating because it did not recognise yours | Use the commands or placeholders your slicer checks for (table below) |
| The temperature is wrong for a new filament | A fixed number in the start code | Replace every number with a placeholder |
| The nozzle oozes onto the bed while probing | Probing at full printing temperature | Probe at a lower temperature, heat afterwards |
| The first layer is uneven after you edited the start | Probing before the bed reached temperature | Put M190 before the probing command |
| A strand is dragged into the first layer | The purge ended without a wipe or lift, or the line peeled off | Wipe and lift after the purge, or print a skirt |
| The nozzle scrapes the print at the end | A sideways move before the lift, or homing Z | Lift first; never home Z at the end |
| Klipper ignores your saved mesh | It no longer loads one at start-up | Add BED_MESH_PROFILE LOAD=default to START_PRINT |
Where to edit it, and which placeholders each slicer reads
A placeholder is a slicer variable that becomes a value when you slice. Each slicer has its own names and brackets, so code copied between slicers needs converting.
Where start G-code lives and what stops the slicer adding its own heating
| Slicer | Where to edit | First-layer nozzle | First-layer bed | It adds its own heating unless |
|---|---|---|---|---|
| PrusaSlicer 2.9 | Printer Settings > Custom G-code (expert mode) | [first_layer_temperature] | [first_layer_bed_temperature] | Your code contains M104, M109, M140, M190, M141 or M191 |
| OrcaSlicer 2.5 | Printer settings > Machine G-code | {first_layer_temperature[initial_extruder]} | {bed_temperature_initial_layer_single} | Bed: your code has M140 or M190. Nozzle: it has M104 or M109. With the Klipper flavour it adds none |
| Bambu Studio 2.9 | Printer settings > Machine gcode | {nozzle_temperature_initial_layer[initial_extruder]} | [bed_temperature_initial_layer_single] | Same rule as OrcaSlicer |
| UltiMaker Cura 5.14 | Preferences > Configure Cura > Printers > Machine Settings | {material_print_temperature_layer_0} | {material_bed_temperature_layer_0} | It finds one of its temperature placeholders; a typed M190 S60 does not count |
Without temperature commands in your code, PrusaSlicer heats the bed and waits, starts the nozzle, runs your code, then waits for the nozzle; Emit temperature commands automatically, under the start G-code field, switches that off. PrusaSlicer's macros also do arithmetic and conditions, which Prusa's profile uses to pick the probing temperature per filament. Cura has accepted formulas since 5.6 and if/else conditions since 5.9, so older advice that it cannot calculate in start code is out of date.
Teaching Tech's video on slicer variables has a chapter on start G-code temperatures, and the link starts there. Its chapter on Cura's limits predates Cura 5.6.
Heating order: why the bed comes first and the nozzle stays cooler
Heaters take longest, so start them early. M140 and M104 set a target and move on; M190 and M109 wait. Klipper's sample macro starts the bed with M140 before G28, so the bed warms while the printer homes.
Wait for the bed before you probe. A bed that is still heating changes shape, which is why Prusa's MK4S and Bambu Lab's P1S profiles both wait with M190 before they measure. Some start codes shared on the Bambu Lab forum skip this wait to save time; the bed then keeps changing after it has been measured. For stubborn first layers, Bambu Lab's H2D guide suggests holding the bed at temperature for 10 to 15 minutes.
Keep the nozzle cooler while probing, so it does not ooze onto the bed or the probe. Prusa's MK4S code holds the nozzle at 170 °C for PLA and 175 °C for PETG while it homes and probes, and 25 °C below the first-layer temperature for PC and nylon. Bambu Lab's P1S code cools it to 140 °C before levelling. Both heat up to printing temperature again only after probing, just before the purge line.
Four published start sequences compared
| Step | Klipper sample START_PRINT | Prusa MK4S (PrusaSlicer) | Bambu Lab P1S (Bambu Studio) | Cura profile for Creality |
|---|---|---|---|---|
| Modes | G90 | G90, then M83 | G90; M83 before the purge | None; Cura sets absolute mode |
| Bed | M140 first, M190 after homing | M140 first, M190 before probing | M140 and M190 first of all | Added by Cura |
| Nozzle while probing | No probing in the sample | 170 °C for PLA, 175 °C for PETG | 140 °C | No probing |
| Bed measuring | None | Only the area the print uses | Only the area the print uses | None |
| Purge | None | Line along the front at Y -4, then a wipe | Flushes 105 mm at the back, then a line along the front edge | Two lines along the left edge |
Where to measure and when to re-run a mesh is covered in the bed levelling guide.
Worked example: Cura's Creality start code with heating you control
Cura's Creality profile has no heating lines, so Cura adds its own before it. To set the order yourself, add the four heating lines at the top. Because they use Cura's own placeholders, Cura drops its automatic heating.
A Creality start code with your own heating order
| Line | What it does |
|---|---|
| M140 S{material_bed_temperature_layer_0} | Starts the bed and carries on |
| G28 | Homes while the bed warms |
| M190 S{material_bed_temperature_layer_0} | Waits for the bed |
| M109 S{material_print_temperature_layer_0} | Heats the nozzle and waits |
| G92 E0 | Sets the extruder to zero |
| G1 Z2.0 F3000 | Lifts 2 mm |
| G1 X10.1 Y20 Z0.28 F5000.0 | Moves to the start of the purge |
| G1 X10.1 Y200.0 Z0.28 F1500.0 E15 | Draws a 180 mm line at 25 mm/s, feeding 15 mm of filament |
| G1 X10.4 Y200.0 Z0.28 F5000.0 | Steps 0.3 mm aside |
| G1 X10.4 Y20 Z0.28 F1500.0 E30 | Draws the line back; E30 means another 15 mm in absolute mode |
| G92 E0 | Sets the extruder to zero again |
| G1 Z2.0 F3000 | Lifts before the first travel |
If your printer has a probe, add your probing command after M190 and heat the nozzle afterwards, as in the profiles above. Test any edit with a small print while you watch the start.
The purge line, and why it ends up in your print
A purge line pushes out old filament and fills the nozzle before the first layer. The classic complaint is the strand the first travel drags onto the bed. The accepted answer on the 3D Printing Stack Exchange offers a skirt or brim, which primes without any G-code, or a priming line in the start code. Another answer adds two rules: swipe in one direction only, so you do not pick the purged plastic up again, and do not over-extrude, so no pressure is left in the nozzle.
Prusa ends its purge with a wipe: the nozzle dips to 0.05 mm and moves away fast. A P1S owner on the Bambu Lab forum stopped the return pass of the front prime line short, so the first pass keeps its grip; that is a user change, not Bambu advice.
Teaching Tech shows how slicer variables can place the purge next to the object instead of at a fixed edge, which keeps it short and close to where printing starts.
If the purge line does not stick, fix the first layer, not the G-code: see the first layer guide. For flush volumes in multicolour printing, see the purge waste guide.
Klipper: put the sequence in START_PRINT and END_PRINT
On Klipper, keep the real sequence in macros in the printer configuration, and pass only the temperatures from the slicer. Klipper's own examples are START_PRINT BED_TEMP={material_bed_temperature_layer_0} EXTRUDER_TEMP={material_print_temperature_layer_0} in Cura and START_PRINT EXTRUDER_TEMP=[first_layer_temperature] BED_TEMP=[first_layer_bed_temperature] in PrusaSlicer. Changing the macro then needs no re-slicing.
Klipper advises PrusaSlicer users to put M140 S0 and M104 S0 before the macro call, so the slicer adds no heating of its own; in OrcaSlicer and Bambu Studio, the Klipper G-code flavour does the same. Since 1 February 2023, Klipper no longer loads a saved bed mesh at start-up, so add BED_MESH_PROFILE LOAD=default to START_PRINT if you use one.
End G-code: lift, park and cool down
All four switch the heaters off and get the nozzle clear of the print. They differ in how far they lift, where they park and what stays powered.
Four published end sequences compared
| Step | Klipper sample END_PRINT | Prusa MK4S | Bambu Lab P1S | Cura profile for Creality |
|---|---|---|---|---|
| Retract | 3 mm while moving 2 mm aside | None in the end code | 0.8 mm | 2 mm, then 2 mm more |
| First lift | 10 mm | To 1 mm above the top layer | To 0.5 mm above the top layer | 0.2 mm, then 10 mm |
| Park | No park | X 241, Y 170 | Back of the printer | Bed to the front with G1 X0 Y{machine_depth} |
| Heaters and fan off | First, before moving | After the first lift | Bed and fans after the park, hotend last | After the park |
| Final lift | None | To 23 mm above the top layer | 100 mm, up to Z 250 | None |
| Motors | M84 | M84 X Y E, Z stays on | Lowered current | M84 X Y E, Z stays on |
Do not put a plain G28 in end code. On many printers it homes Z too and drives the nozzle into the print. Move away with G1 instead, as Cura's Creality code does.
Let the hotend cool before you cut the power. On Marlin, the hotend fan runs from the measured temperature, not the target, and keeps the heat sink cool until the hotend is below 50 °C. Cutting the power at once stops that fan; the heat creep guide explains what follows when the cold side gets warm.
When to stop
Switch the printer off at once if a move pushes the nozzle into the bed or an axis runs against its end; a wrong Z value in a purge move is the usual cause, and the nozzle height itself belongs to the z-offset guide. Forum code is written for one printer and one profile version, so compare the date line at the top of your own start code before you use someone else's.
Next steps and related guides
What each command does, and where Marlin, Klipper and Prusa firmware differ, is in the G-code commands guide. If you are moving your sequence into Klipper macros, the Klipper guide explains what else changes on the printer.
Sources
PrusaSlicer profile bundle 2.5.16 (MK4S start and end G-code)
3D Printing Stack Exchange: Writing G-code: swiping at start of print
3D Printing Stack Exchange: Move print bed to front at the end of a print rather than the back
3D Printing Stack Exchange: Using auto-bed leveling, do I need to initiate G29 before every print?
Bambu Lab Forum: FIXED: prime line getting pulled into print
Bambu Lab Forum: Optimized A1 mini Start G-code (Quick and Quiet version)

