Ships (and space stations) can be scrapped. In order to do it select a ship or an entire fleet you want to scrap and give it the single turn SCRAP order.
If it's scrapped inside one of the own systems, the industry costs used to build the ship are partially returned in form of credits. The resources spent e.g. for the ship's hull are put back into the storage of the system where the ship was scraped. With an advanced Shipyard the recycling efficiency is greater, while when the ship is damaged, you get less resources (you should Repair them first).
For scrapping buildings see here Scrap Buildings.