Copyright doesn’t go away simply because you edit what was copied, deleting it may work assuming you don’t recreate to much of the original work.
There is a concept in copyright law of derivative work which means simply editing Harry Potter isn’t enough to publish it without paying for the rights to do so. Even highly transformative fanficfion can run into this issue when much of the original work’s characters and setting remain.
Disney gets away with this stuff by copying public domain works, copilot and it’s users don’t have that defense.
The key word is fan fiction. Almost every single word in fiction is a creative expression and thus copyrightable.
But works of non-fictional literature (of which source code is an example) have to work harder to justify their copyright.
Many of the snippets of code generated by copilot contain zero creative expression. They are just mechanical implementations of an algorithm.
You mentioned setting and plot. The source code equivalent is architecture. Copilot generated snippets are usually too short and too algorithmic to have their own architecture.
What makes a derivative work derivative is that it copies creative expressions from the parent work. If you edit them out, then it’s not a derivative work anymore.
Variable names are just as subject to copyright as character names, and specific implications of algorithms can vary quite a bit. Just look at the homework assignments of any introductory programming course.
Which is why even very short snippets of code have been an issue.
There is a concept in copyright law of derivative work which means simply editing Harry Potter isn’t enough to publish it without paying for the rights to do so. Even highly transformative fanficfion can run into this issue when much of the original work’s characters and setting remain.
Disney gets away with this stuff by copying public domain works, copilot and it’s users don’t have that defense.