How to delete container with lxc (LXD) command on Linux

Want to delete LXD based container with lxc command on Linux? Try these simple tips. To delete LXD instances/container and snapshots, we use the lxc delete command. However, the container/instance must be stopped, and all snapshots must be removed before removing the Linux container with the lxc command.

Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements LXD with lxc on Linux
Time 2m
Let us see syntax and example to delete container with lxc.

Syntax

The syntax is simple:
lxc delete instance
lxc delete instance/snapshot
lxc delete instance {options}
## rm is alias for delete ##
lxc rm container/snapshot

Listing all containers / instances

Open the terminal application and then type the following command:
lxc list
lxc ls
## list container name, state and snapshots ##
lxc list -c nsS

List of all my LXD instances:
+--------------------+---------+-----------+
|        NAME        |  STATE  | SNAPSHOTS |
+--------------------+---------+-----------+
| alpine             | RUNNING | 0         |
+--------------------+---------+-----------+
| arch               | STOPPED | 1         |
+--------------------+---------+-----------+
| c1                 | STOPPED | 0         |
+--------------------+---------+-----------+
| centos-6           | STOPPED | 0         |
+--------------------+---------+-----------+
| centos-7           | STOPPED | 0         |
+--------------------+---------+-----------+
| centos-8           | RUNNING | 0         |
+--------------------+---------+-----------+
| debdocker          | RUNNING | 0         |
+--------------------+---------+-----------+
....
.....
..
| opensusenginx-test | STOPPED | 0         |
+--------------------+---------+-----------+
| oracle-7           | STOPPED | 0         |
+--------------------+---------+-----------+
| plamo              | STOPPED | 0         |
+--------------------+---------+-----------+
| sabayon            | STOPPED | 0         |
+--------------------+---------+-----------+
| ubuntu-20-4        | STOPPED | 0         |
+--------------------+---------+-----------+
| ubuntunginx-test   | STOPPED | 0         |
+--------------------+---------+-----------+

WARNING: The following commands will delete the LXD instance/container including all snapshots. Always back up all critical data, including LXD containers, as this action is irreversible. See how to backup and restore LXD instance for more info.

Deleting container with lxc command on Linux for LXD

Let us remove lxd instance named arch, run:
lxc delete arch
Verify it:
lxc list
You will get an error on screen as follows when the instance is running on Linux and you type ‘lxc delete’ command:

Error: The instance is currently running, stop it first or pass –force

To get rid of this problem either stop the Linux container or pass the --force option:
lxc stop {container1} && lxc delete {container1}
## OR ##
lxc delete {container1} --force

Removing container snapshot with lxc command

We can create a snapshot of “alpine” called “snap0” as follows:
lxc snapshot alpine snap0
Display info about the instance, enter:
lxc info alpine
Delete a snapshot of “alpine” called “snap0”, run:
lxc delete alpine/snap0
## verify it with the help of egrep command ##
lxc list -c nsS | egrep 'NAME|STATE|alpine'

Conclusion

You learned how to remove LXD based instance/container using the lxc command on Linux. See help as follows:
lxc help delete

Description:
  Delete instances and snapshots

Usage:
  lxc delete [:][/] [[:][/]...] [flags]

Aliases:
  delete, rm

Flags:
  -f, --force         Force the removal of running instances
  -i, --interactive   Require user confirmation

Global Flags:
      --debug            Show all debug messages
      --force-local      Force using the local unix socket
  -h, --help             Print help
      --project string   Override the source project
  -q, --quiet            Don't show progress information
  -v, --verbose          Show all information messages
      --version          Print version number

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.