How to stop drush displaying a Drupal error message? This question is a follow-on from this one about creating managed files. If you look at the question in the link, you'll see that I am writing a drush command to detect all the inline-images in a just-migrated Drupal 8 system, and add them into Drupal as managed images attached to the relevant nodes. To cut a long story short, this bit of code takes the existing file name and creates an entry in Drupal:

$file = entity_create('file');
$file-&gt;setFilename($file_name);
$file-&gt;setFileUri('public://images/'.$file_name);
$file-&gt;save();


This works fine, except that it very irritatingly displays this error message with every file: Object(Drupal\Core\Entity\Plugin\DataType\EntityAdapter).uri: The file &lt;em class="placeholder"&gt;public://images/rint135_graph4_0.articleimage.PNG&lt;/em&gt; already exists. Enter a unique file URI. 

Of course the file exists! That is precisely the point! Despite the error message everything seems to work ok, but I would really like to get rid of those error messages. I guess that they are being set by drupal_set_message(), since they include HTML which of course is pointless in drush - but I can't find any way to switch them off.
Two options:


Hide the messages with ob_start() and ob_end().
Avoid the messages by moving the target file out of the way, and moving it back again after saving the entity.