At first I thought it was working, later I have found a bug in the code. Should it stay or should it be deleted?
Since you've thought it was all working prior to posting it, it is okay. Consider this snippet from the FAQ:
To the best of my knowledge, does the code work?
The non-working aspect of the code is still off-topic, though. But this shouldn't warrant deletion in and of itself. With the first point in mind, and the intent of the post not solely being about fixing a bug, it can remain here. However, such bug(s) should still be addressed on Stack Overflow.