How to delete a product's base image programatically and then set the next image as base?

A post was split to a new topic: «addImageToMediaGallery() ‘image’, ‘small_image’, ‘thumbnail’ not selected in image»