Clear NVS storage causes device reboot (nrf5340)

Im trying to clear NVS, it gets cleared but then causes device to reboot. 

code in question is :

void clearNVS() {
  int          err;
  const size_t nvs_partition_size   = PM_NVS_STORAGE_SIZE;
  const off_t  nvs_partition_offset = PM_NVS_STORAGE_ADDRESS;

  if (!device_is_ready(fs.flash_device)) {
    printk("Flash device %s is not ready\n", fs.flash_device->name);
    return;
  }


  printk("Erasing NVS flash memory (0x%08lx, size: %u bytes)...\n",
    (unsigned long)nvs_partition_offset, (unsigned int)nvs_partition_size);
  k_sleep(K_MSEC(500));

  err = nvs_clear(&fs);
  if (err < 0) {
    printk("Failed to erase NVS flash memory: %d\n", err);

    if (conn) {
      char response_buffer[negotiated_MTU];
      snprintf(response_buffer, sizeof(response_buffer),
        "Failed to erase NVS flash memory: %d\n", err);
      send_FS_resp(response_buffer);
    }
    return;
  }

  k_sleep(K_MSEC(500));
  printk("NVS flash memory erased successfully.\n");
  k_sleep(K_MSEC(500));

  if (conn) {
    send_FS_resp("NVS flash memory erased successfully.\n");
  }
  k_sleep(K_MSEC(500));


  err = nvs_mount(&fs);
  if (err < 0) {
    printk("Failed to reinitialize NVS: %d\n", err);
    if (conn) {
      char response_buffer[negotiated_MTU];
      snprintf(response_buffer, sizeof(response_buffer),
        "Failed to reinitialize NVS: %d\n", err);
      send_FS_resp(response_buffer);
    }
  } else {
    printk("NVS reinitialized successfully after erase.\n");
  }
}

output halts after 
Erasing NVS flash memory (0x000f8000, size: 24576 bytes)...

Regards, 

Related