> For the complete documentation index, see [llms.txt](https://www.learnros2.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.learnros2.com/ros/cookbook/how-to-add-and-remove-models-in-gazebo-simulation-dynamically.md).

# How to add and remove models in Gazebo simulation dynamically

In this article, we will present how we can add and remove models in Gazebo simulation dynamically. By dynamically, we meana the ability to add and remove models after the Gazebo process is launched.

Suppose we define a world called `demo` in the `sdf` file and launch a Gazebo instance with it. When the Gazebo is up and running, we should see a few services are created automatically. For instance, if we run the command:

```
ign service -l | grep "world"
```

It produces the following output:

```
/gazebo/worlds
/world/demo/control
/world/demo/control/state
/world/demo/create
/world/demo/create_multiple
/world/demo/declare_parameter
/world/demo/disable_collision
/world/demo/enable_collision
/world/demo/entity/system/add
/world/demo/generate_world_sdf
/world/demo/get_parameter
/world/demo/gui/info
/world/demo/level/set_performer
/world/demo/light_config
/world/demo/list_parameters
/world/demo/playback/control
/world/demo/remove
/world/demo/scene/graph
/world/demo/scene/info
/world/demo/set_parameter
/world/demo/set_physics
/world/demo/set_pose
/world/demo/set_pose_vector
/world/demo/set_spherical_coordinates
/world/demo/state
/world/demo/state_async
/world/demo/system/info
/world/demo/visual_config
/world/demo/wheel_slip

```

Our focus in this article is on `/world/demo/create` and `/world/demo/remove`.

The command we will use is:

{% code overflow="wrap" %}

```
ign service -s <service-name> --reqtype <request-type> --reptyp <response-type> --timeout <timeout> --req <request>
```

{% endcode %}

{% hint style="warning" %}
It's recommended to use one-line command. You may run into issues if you break the command into multiple lines with "\\"
{% endhint %}

To find out the request type and response type of the service, we can use command `ign service -is`. For example:

```
ign service -is /world/demo/create
```

It produces:

```
Service providers [Address, Request Message Type, Response Message Type]:
  tcp://10.0.0.22:32865, ignition.msgs.EntityFactory, ignition.msgs.Boolean
```

We can check the detials of the message with command `ign msg -i`. For example:

```
ign msg -i ignition.msgs.EntityFactory
```

and it produces:

```
Name: ignition.msgs.EntityFactory
File: ignition/msgs/entity_factory.proto

message EntityFactory {
  .ignition.msgs.Header header = 1;
  oneof from {
    string sdf = 2;
    string sdf_filename = 3;
    .ignition.msgs.Model model = 4;
    .ignition.msgs.Light light = 5;
    string clone_name = 6;
  }
  .ignition.msgs.Pose pose = 7;
  string name = 8;
  bool allow_renaming = 9;
  string relative_to = 10;
  .ignition.msgs.SphericalCoordinates spherical_coordinates = 11;
}
```

As expected, we need to provide the following information to spawn a new model instance:

* name: Model nam. This is also the "identifier" of the model instance and needs to be unique.
* model source: In this article, we use sdf\_filename option and will provide a path of the model `sdf` file in the request.
* pose: The pose of the model instance.

Examine the `.ignition.msgs.Pose` message type and it shows we can specify both position and the orientation of the model instance.

```
Name: ignition.msgs.Pose
File: ignition/msgs/pose.proto

message Pose {
  .ignition.msgs.Header header = 1;
  string name = 2;
  uint32 id = 3;
  .ignition.msgs.Vector3d position = 4;
  .ignition.msgs.Quaternion orientation = 5;
}

```

The command below creates a new model instance in Gazebo with the following attributes:

* The model name is test
* The model definition `sdf` file is `diff_drive/model.sdf`
* The initial pose is at (10, 10, 0) with orientation (0, 0, 0.7071, 0.7071) (i.e. theta=90 degree)

{% code overflow="wrap" %}

```
ign service -s /world/demo/create --reqtype ignition.msgs.EntityFactory --reptype ignition.msgs.Boolean --timeout 5000 --req 'name: "test"; sdf_filename: "diff_drive/model.sdf"; pose: {position: {x: 10, y: 10, z: 0}, orientation: {x: 0, y: 0, z: 0.7071, w: 0.7071}}'
```

{% endcode %}

To remove the model instance, we can send a request to `/world/demo/remove` service. For instance:

{% code overflow="wrap" %}

```
ign service -s /world/demo/remove --reqtype ignition.msgs.Entity --reptype ignition.msgs.Boolean --timeout 5000 --req 'type: MODEL; name: "test"'
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.learnros2.com/ros/cookbook/how-to-add-and-remove-models-in-gazebo-simulation-dynamically.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
