# Code to replace and delete single element in the list

**URL:** https://dmap-community.ductus.global/t/code-to-replace-and-delete-single-element-in-the-list/3106
**Category:** YANG
**Created:** [June 12, 2020, 3:49pm UTC](https://dmap-community.ductus.global/t/code-to-replace-and-delete-single-element-in-the-list/3106 "2020-06-12T15:49:10Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![vikas](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@vikas](https://dmap-community.ductus.global/u/vikas)
#### Post date: [June 12, 2020, 3:49pm UTC](https://dmap-community.ductus.global/t/code-to-replace-and-delete-single-element-in-the-list/3106/1 "2020-06-12T15:49:10Z")

</div>

1. how to delete a single element in the list?
2. how can we replace the element in the list if is already present in the list based on the order?

---

<div class="post-metadata">

### Author: ![cohult](https://yyz2.discourse-cdn.com/flex010/user_avatar/dmap-community.ductus.global/cohult/32/221_2.png) [@cohult](https://dmap-community.ductus.global/u/cohult)
#### Post date: [June 12, 2020, 8:42pm UTC](https://dmap-community.ductus.global/t/code-to-replace-and-delete-single-element-in-the-list/3106/2 "2020-06-12T20:42:00Z")

</div>

1. config (config true) or operational state (config false) data?
2. Application interfaces (e.g. MAAPI or CDB) or northbound interfaces, (e.g. NETCONF, RESTCONF, CLI, etc.)?

---

<div class="post-metadata">

### Author: ![vikas](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@vikas](https://dmap-community.ductus.global/u/vikas)
#### Post date: [June 14, 2020, 4:38am UTC](https://dmap-community.ductus.global/t/code-to-replace-and-delete-single-element-in-the-list/3106/3 "2020-06-14T04:38:04Z")

</div>

how config true will delete a element in the list, can you please share an example?

---

<div class="post-metadata">

### Author: ![cohult](https://yyz2.discourse-cdn.com/flex010/user_avatar/dmap-community.ductus.global/cohult/32/221_2.png) [@cohult](https://dmap-community.ductus.global/u/cohult)
#### Post date: [June 14, 2020, 7:46am UTC](https://dmap-community.ductus.global/t/code-to-replace-and-delete-single-element-in-the-list/3106/4 "2020-06-14T07:46:12Z")

</div>

You didn’t answer the second question, but you did say “code” in the header for this topic. "code\* and config true data would mean “MAAPI” to me, so based on the example in the RFC 7950:

- [7.8.7](https://tools.ietf.org/html/rfc7950#section-7.8.7). Usage Example (lists NETCONF)

Given the following list:

```java
module example-config {
  yang-version 1.1;
  namespace "urn:example:config";
  prefix "co";

  revision 2020-06-14;
  
  list user {
    key "name";
    ordered-by user;
    description
      "This is a list of users in the system.";

    leaf name {
      type string;
    }
    leaf type {
      type string;
    }
    leaf full-name {
      type string;
    }
  }
}

```

With the following config:

```xml
  <config xmlns="http://tail-f.com/ns/config/1.0">
     <user xmlns="urn:example:config">
       <name>wilma</name>
       <type>admin</type>
       <full-name>Wilma Flintstone</full-name>
     </user>
     <user xmlns="urn:example:config">
       <name>barney</name>
       <type>admin</type>
       <full-name>Barney Rubble</full-name>
     </user>
     <user xmlns="urn:example:config">
       <name>fred</name>
       <type>admin</type>
       <full-name>Fred Flintstone</full-name>
     </user>
  </config>

```

To delete user "barney\* using the MAAPI C-API:  
maapi\_delete(sock, tid, “/user{barney}”);

Using the confd\_cmd tool that in turn use maapi\_delete():

```auto
$ confd_cmd -dd -c 'mdel "/co:user{barney}"'
mdel "/co:user{barney}"
TRACE Connected (maapi) to ConfD
TRACE MAAPI_START_USER_SESSION --> CONFD_OK
TRACE MAAPI_START_TRANS --> CONFD_OK
TRACE MAAPI_DELETE /co:user{barney} --> CONFD_OK
TRACE MAAPI_APPLY_TRANS --> CONFD_OK
TRACE MAAPI_STOP_TRANS --> CONFD_OK
TRACE MAAPI_END_USER_SESSION --> CONFD_OK

```

See `$CONFD_DIIR/src/confd/tools/confd_cmd.c` for source code

---

<div class="post-metadata">

### Author: ![cohult](https://yyz2.discourse-cdn.com/flex010/user_avatar/dmap-community.ductus.global/cohult/32/221_2.png) [@cohult](https://dmap-community.ductus.global/u/cohult)
#### Post date: [June 14, 2020, 8:08am UTC](https://dmap-community.ductus.global/t/code-to-replace-and-delete-single-element-in-the-list/3106/5 "2020-06-14T08:08:54Z")

</div>

To insert a list entry you first create it using the MAAPI C-API `maapi_create()`, then set the “full-name” using `maapi_set()`, and lastly move the new entry after “wilma” using `maapi_move_ordered()`

Example using confd\_cmd that use the above maapi functions:

```bash
$ confd_cmd -dd -c 'mcreate "/co:user{betty}"; mset /co:user{betty}/full-name "Betty Rubble"; mset /co:user{betty}/type admin; mmove "/co:user" "{betty}" after "wilma"'
mcreate "/co:user{betty}" ; mset "/co:user{betty}/full-name" "Betty Rubble" ; mset "/co:user{betty}/type" "admin" ; mmove "/co:user" "{betty}" "after" "wilma"
TRACE Connected (maapi) to ConfD
TRACE MAAPI_START_USER_SESSION --> CONFD_OK
TRACE MAAPI_START_TRANS --> CONFD_OK
TRACE MAAPI_CREATE /co:user{betty} --> CONFD_OK
TRACE MAAPI_SET_ELEM2 /co:user{betty}/full-name --> CONFD_OK
TRACE MAAPI_SET_ELEM2 /co:user{betty}/type --> CONFD_OK
TRACE Connected (maapi) to ConfD
TRACE MAAPI_LOAD_ALL_NS
TRACE MAAPI_LOAD_MNS_MAPS
TRACE MAAPI_LOAD_HASH_DB
TRACE MAAPI_MOVE_ORDERED /co:user{betty} --> CONFD_OK
TRACE MAAPI_APPLY_TRANS --> CONFD_OK
TRACE MAAPI_STOP_TRANS --> CONFD_OK
TRACE MAAPI_END_USER_SESSION --> CONFD_OK

```

```xml
$ confd_load -dd -Fp -p /co:user 
TRACE Connected (maapi) to ConfD
starting user session ctxt=system user=system groups=[system]
TRACE MAAPI_START_USER_SESSION --> CONFD_OK
TRACE MAAPI_START_TRANS --> CONFD_OK
TRACE MAAPI_SAVE_CONFIG --> CONFD_OK
TRACE Connected (stream) to ConfD
<config xmlns="http://tail-f.com/ns/config/1.0">
  <user xmlns="urn:example:config">
    <name>wilma</name>
    <type>admin</type>
    <full-name>Wilma Flintstone</full-name>
  </user>
  <user xmlns="urn:example:config">
    <name>betty</name>
    <type>admin</type>
    <full-name>Betty Rubble</full-name>
  </user>
  <user xmlns="urn:example:config">
    <name>fred</name>
    <type>admin</type>
    <full-name>Fred Flintstone</full-name>
  </user>
TRACE MAAPI_SAVE_CONFIG_RESULT --> CONFD_OK
</config>
TRACE MAAPI_END_USER_SESSION --> CONFD_OK

```
