Related to this question, but not the same since these classes have a different wording.
For an Arcane Trickster or Eldritch Knight the replacing spells option is not listed under Spells Known of 1st level or Higher (as it is in other classes). So, can a character in these classes replace cantrips with other spells?
Spells Known: […] Whenever you gain a level in this class, you can replace one of the wizard spells you know with another spell of your choice from the wizard spell list (of a level you can cast).
Best Answer
There isn't a provision in the rules to replace a cantrip once chosen
Also, from the lead dev, there is this statement of intent regarding the Eldritch Knight:
The above is consistent with the general absence of any rules text in the PHB addressing the replacing of a cantrip as a character goes up in level. By that absence, by the lack of provision to replace any cantrip in any class upon leveling up, the general case is that once chosen a cantrip is yours forever. The "choosing 1st level spells and higher" feature has a specific way to change one's spell list as one goes up in level. Level 0 (cantrip) spells don't share that feature for any class.
This matches how a cantrip's power goes up with your character level. Let's look at fire bolt:
The concept is that you stick with a cantrip forever, and that it gets stronger as you (the character) get more powerful as you move into successively more challenging tiers of play. (p. 10, PHB; Adventure Tiers 2, 3, and 4 start at levels 5, 11, and 17).
Crawford also offers this advice in two different tweets. The choice is generally permanent:
The cantrip is a unique kind of spell.
A DM can rule otherwise
In my first campaign, when some new material came out for Temple of Elemental Evil, my DM allowed me to replace one of the cleric cantrips I had chosen, Spare the Dying, with one that was in the EE Supplement: Thunderclap. The decision was thematic due to being a tempest cleric.