Delete LXC Container after moving to another storage

Hi,

I’ve moved a container to another storage by

lxc move container-a container-temp
lxc container-temp -s secondpool

then I would like to delete the container-temp.

lxc delete container-temp
Error: Cannot remove an instance volume that has snapshots

I tried to create a new snapshot with

sudo zfs snapshot default/containers/container-temp@snapshot-snap0

and afterwords I tried to delete the container-temp angain without success.

in my despair I delete the ZFS of the whole container-temp

sudo zfs destroy default/containers/container-temp

The container is still listed in LXC.

Any ideas?

You should avoid making changes to the storage pools directly as this can cause the meta data in LXD to get out of sync with reality and cause confusion and issues.

Instead lxc info <instance> would show the snapshots you have, then lxc delete <instance>/<snapshot> would delete them.

Here is the output

lxc info container-temp

Name: container-temp
Location: none
Remote: unix://
Architecture: x86_64
Created: 2019/04/28 07:11 UTC
Status: Stopped
Type: container
Profiles: default_100
Snapshots:
snap0 (taken at 2019/10/04 15:03 UTC) (stateless)

lxc delete container-temp/snap0

Error: not found

Yes you’re going to have some trouble removing the entry now because the underlying storage volumes have been manually removed. So LXD’s database has got out of sync with reality.

Try lxd sql global "delete from instances where name = 'container-temp'"

1 Like

Thx

the container is now removed out of the lxc list. Isthere anything else to do to resync the LXD database or to avoid further trobble with my containers or databases?

There is no re-sync capability. The best thing to do is just use the lxc commands to perform storage operations rather than doing them directly using the underlying tools.

1 Like

One more thing

if I try to launch a new container-temp I got this error message

#: lxc launch ubuntu:20.04 container-temp
Creating container-temp
Error: Failed instance creation: Failed creating instance record: Failed initialising instance: Failed creating storage record: UNIQUE constraint failed: storage_volumes.storage_pool_id, storage_volumes.node_id, storage_volumes.project_id, storage_volumes.name, storage_volumes.type

Ah looks like you need to delete the old entries from the storage_volumes table too, take a look at:

lxd sql global "select * from storage_volumes"

±----±-----------------------------------------------------------------±----------------±--------±-----±------------±-----------±-------------+
| id | name | storage_pool_id | node_id | type | description | project_id | content_type |
±----±-----------------------------------------------------------------±----------------±--------±-----±------------±-----------±-------------+
| 1 | container-nextcloud | 1 | 1 | 0 | | 1 | 0 |
| 3 | container-apache | 1 | 1 | 0 | | 1 | 0 |
| 6 | container-fhem | 1 | 1 | 0 | | 1 | 0 |
| 8 | container-apache/snap0 | 1 | 1 | 0 | | 1 | 0 |
| 9 | container-fhem/snap0 | 1 | 1 | 0 | | 1 | 0 |
| 10 | container-nextcloud/snap0 | 1 | 1 | 0 | | 1 | 0 |
| 12 | container-fhem/snap1 | 1 | 1 | 0 | | 1 | 0 |
| 13 | container-nextcloud/snap1 | 1 | 1 | 0 | | 1 | 0 |
| 14 | container-nextcloud/snap2 | 1 | 1 | 0 | | 1 | 0 |
| 20 | container-nextcloud/snap3 | 1 | 1 | 0 | | 1 | 0 |
| 21 | container-nextcloud/snap4 | 1 | 1 | 0 | | 1 | 0 |
| 45 | container-testcloud/snap0 | 1 | 1 | 0 | | 1 | 0 |
| 46 | container-nzbget | 2 | 1 | 0 | | 1 | 0 |
| 52 | container-nzbget/snap0 | 2 | 1 | 0 | | 1 | 0 |
| 53 | container-nextcloud/snap5 | 1 | 1 | 0 | | 1 | 0 |
| 54 | container-nextcloud/snap6 | 1 | 1 | 0 | | 1 | 0 |
| 58 | container-nextcloud/snap7 | 1 | 1 | 0 | | 1 | 0 |
| 95 | container-apache/snap1 | 1 | 1 | 0 | | 1 | 0 |
| 96 | container-fhem/snap2 | 1 | 1 | 0 | | 1 | 0 |
| 97 | container-nextcloud/snap8 | 1 | 1 | 0 | | 1 | 0 |
| 98 | container-nzbget/snap1 | 2 | 1 | 0 | | 1 | 0 |
| 147 | container-guacamole | 1 | 1 | 0 | | 1 | 0 |
| 166 | container-nodered | 1 | 1 | 0 | | 1 | 0 |
| 239 | acb8f742bfffa41f924939857b470b1ee6b55314e7219c65a7e982e0319ef3c5 | 1 | 1 | 1 | | 1 | 0 |
| 292 | e0c3495ffd489748aa5151628fa56619e6143958f041223cb4970731ef939cb6 | 1 | 1 | 1 | | 1 | 0 |
±----±-----------------------------------------------------------------±----------------±--------±-----±------------±-----------±-------------+

Yes so you need to delete the row(s) you don’t need that got left behind, i.e the one that is the old container name that you’re trying to recreate.

Although it looks like the revert steps in the subsequent cleanup may have done that for you.

Does trying the container create again work?

I think I need your help again. How can I delete the row(s). i.e.

| 45 | container-testcloud/snap0 | 1 | 1 | 0 | | 1 | 0 |

lxd sql global "delete from storage_volumes where id = 45"
1 Like

Thanks!

It works now!

1 Like