Skip to main content
2 of 2
Added C++ version and headings
Greenonline
  • 1.7k
  • 6
  • 21
  • 32

There are a number of ways, depending on how you want to do it.

Command line

You could use the command line

rosservice call gazebo/delete_model '{model_name: <model_name>}'

In Python

Or something like this in Python:

from gazebo_msgs.srv import DeleteModel # For deleting models from the environment

def del_model( modelName ): # FIXME: Freezes Python, DO NOT USE!
    """ Remove the model with 'modelName' from the Gazebo scene """
    # delete_model : gazebo_msgs/DeleteModel
    del_model_prox = rospy.ServiceProxy('gazebo/delete_model', DeleteModel) # Handle to model spawner
    # rospy.wait_for_service('gazebo/delete_model') # Wait for the model loader to be ready 
    # FREEZES EITHER WAY
    del_model_prox(str(modelName)) # Remove from Gazebo

Note: You need to pass del_model_prox a string, else it will crash. So it is safer to use

del_model_prox(str(modelName))

rather than just

del_model_prox(str(modelName))

References

In C++

From this answer to Problem with Deleting Models

 #include "gazebo/DeleteModel.h"
    remove(std::string model_name)
         {
           ros::NodeHandle nh("");
           ros::service::waitForService("gazebo/delete_model");
           ros::ServiceClient deleteModelClient = nh.serviceClient<gazebo::DeleteModel>("gazebo/delete_model");
           gazebo::DeleteModel deleteModel;
           deleteModel.request.model_name = model_name;
           deleteModelClient.call(deleteModel);
         }

and call it (from main() or where ever), with

remove(<model_name>);

See also gazebo_msgs/DeleteModel Service

File: gazebo_msgs/DeleteModel.srv

Raw Message Definition

string model_name                 # name of the Gazebo Model to be deleted
---
bool success                      # return true if deletion is successful
string status_message             # comments if available
Greenonline
  • 1.7k
  • 6
  • 21
  • 32