如何在 Linux 中删除分区
分区 磁盘
![](/data/attachment/album/202104/30/095353uhtbhm2fqx44aqfo.jpg)


管理分区是一件严肃的事情，尤其是当你不得不删除它们时。我发现自己经常这样做，特别是在使用 U 盘作为实时磁盘和 Linux 安装程序之后，因为它们创建了几个我以后不需要的分区。


在本教程中，我将告诉你如何使用命令行和 GUI 工具在 Linux 中删除分区。



> 
> 警告!
> 
> 
> 删除了分区，就会失去你的数据。无论何时，当你在操作分区时，一定要备份你的数据。一个轻微的打字错误或手滑都可能是昂贵的。不要说我们没有警告你！
> 
> 
> 


### 使用 GParted 删除磁盘分区 （GUI 方法）


作为一个桌面 Linux 用户，你可能会对基于 GUI 的工具感到更舒服，也许更安全。


有 [几个让你在 Linux 上管理分区的工具](https://itsfoss.com/partition-managers-linux/)。根据你的发行版，你的系统上已经安装了一个甚至多个这样的工具。


在本教程中，我将使用 [GParted](https://gparted.org/index.php)。它是一个流行的开源工具，使用起来非常简单和直观。


第一步是 [安装 GParted](https://itsfoss.com/gparted/)，如果它还没有在你的系统中。你应该能够在你的发行版的软件中心找到它。


![](/data/attachment/album/202104/30/095433m4m5oc4cqhfbc4jq.png)


或者，你也可以使用你的发行版的软件包管理器来安装它。在基于 Debian 和 Ubuntu 的 Linux 发行版中，你可以 [使用 apt install 命令](https://itsfoss.com/apt-command-guide/)：



```
sudo apt install gparted

```

安装完毕后，让我们打开 **GParted**。由于你正在处理磁盘分区，你需要有 root 权限。它将要求进行认证，打开后，你应该看到一个类似这样的窗口：


![](/data/attachment/album/202104/30/095433uqy1f7y7sukz1yks.png)


在右上角，你可以选择磁盘，在下面选择你想删除的分区。


接下来，从分区菜单中选择 “删除” 选项：


![](/data/attachment/album/202104/30/095433dm8vmum2vyvmnm2p.png)


这个过程是没有完整完成的，直到你重写分区表。这是一项安全措施，它让你在确认之前可以选择审查更改。


要完成它，只需点击位于工具栏中的 “应用所有操作” 按钮，然后在要求确认时点击 “应用”。


![](/data/attachment/album/202104/30/095434rygy7byuczwd2eu5.png)


点击 “应用” 后，你会看到一个进度条和一个结果消息说所有的操作都成功了。你可以关闭该信息和主窗口，并认为你的分区已从磁盘中完全删除。


现在你已经知道了 GUI 的方法，让我们继续使用命令行。


### 使用 fdisk 命令删除分区（CLI 方法）


几乎每个 Linux 发行版都默认带有 [fdisk](https://man7.org/linux/man-pages/man8/fdisk.8.html)，我们今天就来使用这个工具。你需要知道的第一件事是，你想删除的分区被分配到哪个设备上了。为此，在终端输入以下内容：



```
sudo fdisk --list

```

这将打印出我们系统中所有的驱动器和分区，以及分配的设备。你 [需要有 root 权限](https://itsfoss.com/root-user-ubuntu/)，以便让它发挥作用。


在本例中，我将使用一个包含两个分区的 USB 驱动器，如下图所示：


![](/data/attachment/album/202104/30/095434pv69vwnzwonrzzo5.png)


系统中分配的设备是 `/sdb`，它有两个分区：`sdb1` 和 `sdb2`。现在你已经确定了哪个设备包含这些分区，你可以通过使用 `fdisk` 和设备的路径开始操作：



```
sudo fdisk /dev/sdb

```

这将在命令模式下启动 `fdisk`。你可以随时按 `m` 来查看选项列表。


接下来，输入 `p`，然后按回车查看分区信息，并确认你正在使用正确的设备。如果使用了错误的设备，你可以使用 `q` 命令退出 `fdisk` 并重新开始。


现在输入 `d` 来删除一个分区，它将立即询问分区编号，这与 “Device” 列中列出的编号相对应，在这个例子中是 1 和 2（在下面的截图中可以看到），但是可以也会根据当前的分区表而有所不同。


![](/data/attachment/album/202104/30/095435i6jpszeg6216ss6z.png)


让我们通过输入 `2` 并按下回车来删除第二个分区。你应该看到一条信息：**“Partition 2 has been deleted”**，但实际上，它还没有被删除。`fdisk` 还需要一个步骤来重写分区表并应用这些变化。你看，这就是完全网。


你需要输入 `w`，然后按回车来使这些改变成为永久性的。没有再要求确认。


在这之后，你应该看到下面这样的反馈：


![](/data/attachment/album/202104/30/095435w2ya24xjl2x03v23.png)


现在，使用 `sudo fdisk --list /dev/sdb` 查看该设备的当前分区表，你可以看到第二个分区已经完全消失。你已经完成了使用终端和 `fdisk` 命令来删除你的分区。成功了！


#### 总结


这样，这个关于如何使用终端和 GUI 工具在 Linux 中删除分区的教程就结束了。记住，要始终保持安全，在操作分区之前备份你的文件，并仔细检查你是否使用了正确的设备。删除一个分区将删除其中的所有内容，而几乎没有 [恢复](https://itsfoss.com/recover-deleted-files-linux/) 的机会。




---


via: <https://itsfoss.com/delete-partition-linux/>


作者：[Chris Patrick Carias Stas](https://itsfoss.com/author/chris/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Managing partitions is serious business, especially when you have to remove them. I find myself doing this frequently, especially after using thumb drives as live disks and Linux installers because they create several partitions that I won’t need afterwards.

In this tutorial, I will show you how to remove partitions in Linux using both command line and GUI tools.

Warning!

You delete the partition, you lose your data. Whenever you are playing with partitions, make sure backup your data. A slight typo or slip of finger could prove costly. Don’t say we didn’t warn you!

## Remove disk partition using GParted [GUI Method]

As a desktop Linux user, you probably will be more comfortable and perhaps safer with a GUI-based tool.

There are [several tools that let you manage partitions on Linux](https://itsfoss.com/partition-managers-linux/). Depending on your distribution you will have one or even more such tool already installed on your system.

For this tutorial, I am going to use [GParted](https://gparted.org/index.php). It is a popular open source tool and it’s very easy and intuitive to use.

The first step is [installing GParted](https://itsfoss.com/gparted/) if it isn’t already in your system. You should be able to find it in the software center of your distribution.

![gparted ubuntu software center](https://itsfoss.com/content/images/wordpress/2021/04/gparted-ubuntu-software-center-800x348.png)

Alternatively, you can use your distribution’s package manager for installing it. In Debian and Ubuntu-based Linux distributions, you can [use the apt install command](https://itsfoss.com/apt-command-guide/):

`sudo apt install gparted`


Once installed, let’s open **GParted**. Since you are dealing with disk partitions, you’ll be required to have root access. It will ask for authentication and once it opens you should see a window like this one:

![removing partitions linux 004](https://itsfoss.com/content/images/wordpress/2021/04/removing-partitions-linux-004.png)

On the right-upper corner you can select the disk and in the lower screen the partition you want to remove.

Next, select the option **Delete** from the Partition menu:

![Removing partitions in Linux with GParted](https://itsfoss.com/content/images/wordpress/2021/04/removing-partitions-linux-005.png)

The process is incomplete until you rewrite the partition table. This is a safety measure and it gives you the option to review the changes before confirming it.

To do this just click on the **Apply All Operations **button located in the toolbar and then **Apply** when asked for confirmation.

![Confirm changes to partitions in GParted](https://itsfoss.com/content/images/wordpress/2021/04/removing-partitions-linux-006.png)

After hitting **Apply**, you will see a progress bar and a results message saying that all the operations were successful. You can close the message and the main window and consider your partition completely deleted from our disk.

Now that you are aware of the GUI method, let’s move on to the command line.

## Delete partitions using fdisk command

Almost every Linux distribution comes with [fdisk](https://man7.org/linux/man-pages/man8/fdisk.8.html) by default and we are going to use this tool today. The first thing you need to know is what device is assigned to the disk with the partitions you want to remove. To do that, type the following in the terminal:

`sudo fdisk --list`


This will print all the drives and partitions in our system as well as the assigned devices. You [need to have root access](https://itsfoss.com/root-user-ubuntu/) in order for it work.

In this example, I will work with a USB drive that contains two partitions as shown below:

![Using fdisk for partitions in Linux](https://itsfoss.com/content/images/wordpress/2021/04/removing-partitions-linux-001.png)

The device assigned in the system is /sdb and it has two partitions, sdb1 and sdb2. Now that you identified which device contains the partitions, you can start working on it by using `fdisk`

and the path to the device:

`sudo fdisk /dev/sdb`


This will start `fdisk`

in command mode. You can always press `m`

to see a list of options.

Next, type `p`

and press `Enter`

to view the partition information and confirm that you are using the right device. If the wrong device is in use you can use the `q`

command to exit `fdisk`

and start the procedure again.

Now enter `d`

to delete a partition and it will immediately ask for the partition number, that corresponds to the number listed in the Device column, which in this case are numbers 1 and 2 (as can be seen in the screen capture below) but can and will vary according to the current partition table.

![Remove partitions in Linux using fdisk](https://itsfoss.com/content/images/wordpress/2021/04/removing-partitions-linux-002.png)

Let’s remove the second partition by typing `2`

and pressing `Enter`

. You should see a message saying **“Partition 2 has been deleted**“, but actually, it hasn’t been removed yet. `fdisk`

needs one more step to rewrite the partition table and apply the changes. Safety net, you see.

You need to type `w`

and press `Enter`

to make the changes permanent. No confirmation is asked.

After this, you should receive some feedback like the one here:

![Delete partition in Linux with fdisk](https://itsfoss.com/content/images/wordpress/2021/04/removing-partitions-linux-003.png)

Now, use `sudo fdisk --list /dev/sdb`

to view the current partition table of the device and you can see that the second partition is completely gone. You are done removing your partition using the terminal and `fdisk`

command. Success!

### Wrapping up

And so I end this tutorial on how to remove partitions in Linux using both the terminal and GUI tools. Remember, stay always on the safe side, backup your files before manipulating your partitions and double check that you are using the right device. Deleting a partition will delete everything in it with little to no chance of [recovering](https://itsfoss.com/recover-deleted-files-linux/) it.