Can a landlord refuse to return your deposit if you decided before occupying the unit that you don't want to live there?

I believe he/she can if there is some kind of written agreement. If this is an illegal apartment then, generally the land lords understand that they have no true legal authority over the deposit. He cant do it because you never lived there. The deposite is to replace anything you break after living in the house.