Skip to content

Remove a Device

If you want to remove a Device from the application, there are essentially two steps. You will want to remove the device from the VPN connection and then clean out any entries in the Flask/SQLAlchemy server application.

Below is an example of how to remove a device. For this example, we will say the device id is fido-002:

First, you want to remove the device from the VPN using headscale which is done at the command line of the EC2 server. After ssh'ing into the server (you are likely already there from the previous step), you can run headscale nodes list which will give you a list of the devices registered with headscale. It will look something like this:

2024-09-30T20:33:14Z TRC DNS configuration loaded dns_config={"Nameservers":["1.1.1.1"],"Proxied":true,"Resolvers":[{"Addr":"1.1.1.1"}]}
An updated version of Headscale has been found (0.23.0 vs. your current v0.23.0-alpha3). Check it out https://github.com/juanfont/headscale/releases
ID | Hostname                             | Name                                 | MachineKey | NodeKey | User     | IP addresses                  | Ephemeral | Last seen           | Expiration          | Connected | Expired
1  | 269bda90-4832-4ed9-abb4-bf9042e6c7a5 | 269bda90-4832-4ed9-abb4-bf9042e6c7a5 | [BRqtI]    | [TqujS] | readinet | 100.64.0.1, fd7a:115c:a1e0::1 | false     | 2023-11-08 23:02:24 | 0001-01-01 00:00:00 | offline   | no
2  | dev                                  | server                               | [zr/HP]    | [dlJvI] | readinet | 100.64.0.2, fd7a:115c:a1e0::2 | false     | 2024-09-30 20:32:43 | 0001-01-01 00:00:00 | online    | no
3  | 071fafc1-7c0d-4a51-bc37-431081184001 | 071fafc1-7c0d-4a51-bc37-431081184001 | [upMFG]    | [yiua6] | readinet | 100.64.0.3, fd7a:115c:a1e0::3 | false     | 2023-11-17 10:32:23 | 0001-01-01 00:00:00 | offline   | no
4  | 376bf7d4-6765-4fbe-9c2e-b1f1599029b5 | 376bf7d4-6765-4fbe-9c2e-b1f1599029b5 | [KNOwe]    | [KMEun] | readinet | 100.64.0.4, fd7a:115c:a1e0::4 | false     | 2023-12-06 06:40:08 | 0001-01-01 00:00:00 | offline   | no
5  | 19eb996a-560a-42c9-a10c-69e41b426dcf | 19eb996a-560a-42c9-a10c-69e41b426dcf | [8hVBm]    | [UZHrY] | readinet | 100.64.0.5, fd7a:115c:a1e0::5 | false     | 2024-02-21 17:59:33 | 0001-01-01 00:00:00 | offline   | no
6  | 4587c326-f223-4af8-bf62-aa5f954796e4 | 4587c326-f223-4af8-bf62-aa5f954796e4 | [N2dnN]    | [FZnWJ] | readinet | 100.64.0.6, fd7a:115c:a1e0::6 | false     | 2024-02-21 21:34:02 | 0001-01-01 00:00:00 | offline   | no
7  | c1eebc30-0d4b-4bc5-8605-767e965ab5bd | c1eebc30-0d4b-4bc5-8605-767e965ab5bd | [hpgCM]    | [gGo8C] | readinet | 100.64.0.7, fd7a:115c:a1e0::7 | false     | 2024-02-22 17:25:08 | 0001-01-01 00:00:00 | offline   | no
8  | 82cad6f1-599f-4665-aa33-0d1057286e9b | 82cad6f1-599f-4665-aa33-0d1057286e9b | [kOpO1]    | [Vguzr] | readinet | 100.64.0.8, fd7a:115c:a1e0::8 | false     | 2024-03-07 18:57:44 | 0001-01-01 00:00:00 | offline   | no
9  | fido-002                             | fido-002                             | [rPL+V]    | [0XO3v] | readinet | 100.64.0.9, fd7a:115c:a1e0::9 | false     | 2024-04-02 16:33:20 | 0001-01-01 00:00:00 | offline   | no
  1. You need to 'expire the node' by running headscale nodes expire -i [id from left most column] (the ID in this example would be 9)
  2. Then delete it by running headscale nodes delete -i [id from left most column] (the ID in this example would be 9)

Next, you will want to remove it from the web application database on the server. You will use a Flask shell in the appropriate docker container to run these command. This gives you access to the Flask application and models that are backed by the database through SQL Alchemy. Make sure you have the id of the device.

$ docker exec -it web flask --app readinet.app shell
from sqlalchemy import text
db.session.execute(text("DELETE FROM connectivity_status WHERE device_id = :id"), {"id": "fido-002"})
db.session.execute(text("DELETE FROM device_status WHERE device_id = :id"), {"id": "fido-002"})
db.session.execute(text("DELETE FROM scheduled_events WHERE device_id = :id"), {"id": "fido-002"})
db.session.execute(text("DELETE FROM sensor_executions WHERE device_id = :id"), {"id": "fido-002"})
db.session.execute(text("DELETE FROM historical_events WHERE device_id = :id"), {"id": "fido-002"})
db.session.execute(text("DELETE FROM log_reset_trigger WHERE device_id = :id"), {"id": "fido-002"})
db.session.execute(text("DELETE FROM organization_devices WHERE device_id = :id"), {"id": "fido-002"})
db.session.execute(text("DELETE FROM devices WHERE id = :id"), {"id": "fido-002"})
db.session.commit()
db.session.expire_all()

Obviously, you need to replace the ID in the above example with the UUID of the device you are getting rid of.

Note

Once you are done and want to exit, hit Cntl-D to get out of Python shell

Note

It might be a good idea to restart the web application after running this so that it invalidates any memory cached data and gets a fresh read from the database. On the EC2 server you can do that by running sudo systemctl restart readinet-web.service