I want to make a G-code script for Cura to clean the nozzle every Xth layer or every one minute?

With Cura there is only start and end G-code, so there is no "layer change" G-code like another slicer (e.g. Ideamaker).

Where should I write the code?

How can I define the Layer number?

  • $\begingroup$ just as a side note. Is this something that you would want your printer to do automatically without needing to resort to g-code modifications? I'm asking.. er... for a "friend". $\endgroup$
    – user77232
    Commented Nov 15, 2020 at 17:32
  • $\begingroup$ yes the Printer should do this automatically, you have to write the G-code in the Slicer (Cura) and every time you slice the written G-code will be automatic there! sometimes you have to be careful, if you change the Template the Orginal G-code will be used! just look if you still have the same G-code! $\endgroup$ Commented Dec 14, 2020 at 15:07
  • 2
    $\begingroup$ Not an answer, but if your nozzle is getting dirty during your print, it means there is a significant amount of material that came out of the extruder that did not bond where it was supposed to. This means the part was not printed right, and does not have the structural properties it should. You should probably try to figure out why that's happening and fix it, and if you do, you'll probably find that you no longer need to clean the nozzle. $\endgroup$ Commented Aug 15, 2021 at 16:55

2 Answers 2


Cura now has a setting "Wipe Nozzle Between Layers", located in the Experimental settings group. it requires the installation of a wipe brush or wipe surface that is fixed to the x axis at a certain x position and moves with it the y and z directions. You can find suitable 3D printable nozzle brush holders for most 3D printers.

It does more or less exactly what you are looking for, just that you do not specify nozzle cleaning intervals "every Xth layer or every one minute" as you want but by print volume – see sub-setting "Material Volume Between Wipes". B setting this to a very small value, you can however force a wipe after every single layer, as that is the hardcoded maximum number of wipes that this setting will generate.

For a good article explaining the setting in depth, see "Cura Wipe Nozzle Between Layers" by Martin Lütkemeyer.


There are extensions for Cura that can do almost what you want to do for you (with respect to the layer or height).

You need to add post-processing scripts to the Cura slicer. You do that from the top menu options Extensions -> Post Processing -> Modify G-Code and then add the option of choice.

E.g. for an event at every layer change you should use script "Insert at layer change" and fill out the G-code you want to perform.

There are options to write your own extension, but that would require some software development skills.

Optionally, you could use the "ChangeAtZ" script to add absurd temperature changes of the extruder to post process that later by a simple e.g. Python script (outside Cura) to replace those actions with the cleaning actions you want to perform. But, when you can do that, you could write a script outside Cura to detect the layers (e.g. from the comments or from the layer change command G1 Zxx) and insert that right away.

For inserting this on the basis of time would be very difficult, it is difficult to estimate the time printing actions take to then inject such a script.

  • $\begingroup$ If you want to add markers, just add comments instead of adding absurd temperature changes. $\endgroup$
    – fectin
    Commented Dec 10, 2022 at 15:32
  • $\begingroup$ @fectin That script won't add solely comments, so inserting a absurd/strange/"other than the used" temperature is a viable solution I've used in the past. $\endgroup$
    – 0scar
    Commented Dec 10, 2022 at 23:25
  • $\begingroup$ ahh, gotcha. Maybe use one of the statuses instead, like M113? Or a beep with M300? $\endgroup$
    – fectin
    Commented Dec 11, 2022 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .