Deleting a User in Linux using Python Script Last Updated : 11 May, 2020 Comments Improve Suggest changes Like Article Like Report Deleting a user from your system or server via a python script is a very easy task. You just need to pass the username of the user and the script will remove the details and all the files of that user.This python script uses userdel Linux command to delete the user.You can directly use userdel command if you remember the command but if you don't then the script makes it easy to delete the user. The script we are about to build will ask the username of the user to be deleted as follows, Input: Enter Username: John Output: User successfully deleted with given credentials Before running the script first we will show the user we want to delete. cat /etc/paswd will list all the users on your system or server.John is the user we will be deleting. The below Python code is the script we will be using for our purpose. Python3 1== import os import subprocess import sys import getpass def delete_user(): username = input("Enter Username : ") try: output = subprocess.run(['userdel', username ]) if output.returncode == 0: print("User successfully deleted with given credentials") except: print(f"Failed to delete user.") sys.exit(1) delete_user() After running the script it will prompt for the username we want to delete and after entering the name the same user will be removed from the system. After if we check by typing cat/etc/passwd we can see that the user "John" is no longer on that list. So we have successfully deleted our user. Comment More infoAdvertise with us Next Article Deleting a User in Linux using Python Script N Naveen Chaudhary 1 Follow Improve Article Tags : Python Linux-Unix Write From Home python-utility Python-scripting +1 More Practice Tags : python Similar Reads Add a User in Linux using Python Script Creating a user via command line in Linux is a tedious task. Every time someone joins your organization and you need to type a long Linux command rather than doing this you can create a python script that can ask for you the username and password and create that user for you. Examples: Input : Enter 1 min read Create multiple users using shell script in Linux In Linux, we create users for multiple purposes, so in Linux, it's very common to make new users depending on the tasks. So sometimes we need to create more than one user or multiple users. We can't do it one by one as it would be very time-consuming, so we can use automated scripts to make our task 3 min read Python SQLite - Deleting Data in Table In this article, we will discuss how we can delete data in the table in the SQLite database from the Python program using the sqlite3 module. In SQLite database we use the following syntax to delete data from a table: DELETE FROM table_name [WHERE Clause] To create the database, we will execute the 2 min read How to Delete User in Linux | userdel Command Managing user accounts is an essential aspect of Linux system administration. Understanding how to delete a user in Linux is crucial, whether you need to remove an unused account, revoke access for a departing employee, or clean up your system for security reasons. Here, we will explore the 'userdel 5 min read Telnet Automation / Scripting Using Python Telnet is the short form of Teletype network, which is a client/server application that works based on the telnet protocol. Telnet service is associated with the well-known port number - 23. As Python supports socket programming, we can implement telnet services as well. In this article, we will lea 5 min read Users in Linux System Administration User management is one of the fundamental tasks in Linux systems administration because a user has to go through a series of access controls to keep an environment secure and organized. It provides functionalities that include adding, modifying, and deleting user accounts, assigning privileges, and 8 min read Python Tweepy - Getting the ID of a user In this article we will see how we can get the ID of a user. ID is the unique identification marker of the twitter account. ID is given to the user by Twitter, the user can not select or change their own ID. In order to get the ID we have to do the following : Identify the user screen name of the pr 2 min read Python Tweepy - Getting the screen name of a user In this article we will see how we can get the screen name of a user. Screen name is the username of the twitter account. It is the name that users choose to identify themselves on the network. Many users choose to either use their real name as the basis for their screen name, often in a shortened v 2 min read Get Username by User ID in Linux The need to get user names by User ID is frequently encountered by system administrators in the Linux world. This task is necessary to manage permissions, diagnose, or simply identify users of the Linux system for different management purposes. Fortunately, Linux provides several methods to achieve 3 min read How to Delete files in Python using send2trash module? In this article, we will see how to safely delete files and folders using the send2trash module in Python. Using send2trash, we can send files to the Trash or Recycle Bin instead of permanently deleting them. The OS module's unlink(), remove() and rmdir() functions can be used to delete files or fol 2 min read Like