How to merge two partitions, one is system, the lower offset, and the other destroyable content, the adjacent higher offset, by using gdisk (preferably if none else is better) ?
What type of partition is the main partition? Are the two partitions next to each other?
If you have a separate hard drive/storage device then back up all data first.
Below are separate instructions for fdisk and gparted; https://askubuntu.com/questions/66000/how-to-merge-partitions
I would recommend the gparted route for ease of use, but if you are doing this with a device that you're ssh'd or otherwise only has a TUI then fdisk should work.
The answer on the second link has a good point, you don't want to try this from a currently running partition. Use a liveCD or a run from a thumbdrive.
char disk[5000]; // The disk is an array of "sectors"
std::string_view partition_table[4]; // Traditional MBR table
partition_table[0] = std::string_view( disk+10, disk+110 );
partition_table[1] = std::string_view( disk+110, disk+2110 );
// Now first partition shows 100 characters disk[10]..disk[109]
// Second partition shows 2000 characters disk[110]..disk[2109]
// Partitions do not affect the data on the disk
// It is an error, if the views are overlapping
// Lets "initialize" the parts of disk shown by partitions:
for ( char& s : partition_table[0] ) s = 'a';
for ( char& s : partition_table[1] ) s = 'b';
// With disks the "initialization" is called "formatting"
// and it writes structures of filesystem into the sectors
// What you can do with "gdisk" is:
partition_table[1] = std::string_view(); // "delete" partition
partition_table[0] = std::string_view( disk+10, disk+2110 ); // modify begin and end
// These do not change the data on sectors disk[10]..disk[2109]
// When OS looks for filesystem, it looks from partition_table[0]
// and finds the one that starts at disk[10]
One still has to "expand" the filesystem to use the entire view it is in,
but that is done with a tool that the filesystem must provide.