How to hide comment section present in render array? I have generated an array in Drupal 8 which is created by using viewbuilder in this manner:

 $content = $this-&gt;entityManager()-&gt;getViewBuilder($entity-&gt;getEntityTypeId())&gt;view($entity, 'hardcopy');


And now I want to hide comment section present in this array.

Any idea on how to do this?
The result that you get there is only prepared, the actual rendered content is not set there, you can't remove it there.

The comments are a field, you can hide/show them in the manage view display UI like any other files.

Define a view mode/display for your use case if the default ones don't work for you and then configure it there.

Doing it in code is only possibly in indirect ways. You'd need to set a flag in $content and then implement hook_node_view() alter and remove it there.