Java System clearProperty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

Definition

In Java, System.clearProperty(key) removes the system property named key and returns the value it had, or null if it was not set. Afterwards getProperty(key) returns null (or its default) again.

It is the opposite of setProperty, and the only clean way to make a property "missing" again: setProperty(key, null) throws, and setProperty(key, "") leaves an empty value behind, which is not the same as no value.

A static method of the java.lang.System class, added in Java 5 (JDK 1.5). Call it on the class name, as in System.clearProperty(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass the key; the result is the removed value:

Java
System.setProperty("app.cache", "on");
String removed = System.clearProperty("app.cache");  // "on"
System.getProperty("app.cache");                     // null
CallWhat it does
System.clearProperty("app.cache")Removes the key; returns its old value or null.
System.clearProperty(key) != nulltrue if something was actually removed.
System.setProperty("app.cache", "")Not the same: the key stays, with an empty value.
System.clearProperty("")Throws IllegalArgumentException.

Added in Java 5 (JDK 1.5). Clearing a key that does not exist is allowed and simply returns null.

Library declarations
Java
// java.lang.System
public static String clearProperty(String key)

Parameters

ParameterDescription
keyName of the property to remove. Must not be null or empty.

Return Value

Return ValueDescription
StringThe value the property had before it was removed, or null if it was not set.

Compare with Similar Methods

Ways to undo or reset properties:

MethodHow it differs
setProperty(key, old)Puts back an earlier non-null value.
setProperties(null)Throws away all changes and recreates the startup set.
getProperties().remove(key)Same effect, but no key check and an Object result.

Java Examples

Example 1 — Remove a Property and See What Comes Back

Set a property, clear it, then clear it again.

Java
public class SystemClearPropertyBasic {
    public static void main(String[] args) {
        System.setProperty("app.cache", "on");
        String removed = System.clearProperty("app.cache");
        String again = System.clearProperty("app.cache");

        System.out.println("Removed value: " + removed);
        System.out.println("Value now: " + System.getProperty("app.cache"));
        System.out.println("Second clear returned: " + again);
    }
}

How It Works

  1. The removed value comes back. "app.cache" held "on", so clearProperty deletes it and returns "on".
  2. Gone, not empty. After clearing, getProperty("app.cache") returns null, exactly as if the key had never been set.
  3. Clearing twice is harmless. The second call finds nothing to remove and returns null instead of throwing.

Example 2 — Turn Feature Flags Off Again

Boolean.getBoolean and Integer.getInteger read system properties. Clearing the keys brings back their default behaviour.

Java
public class SystemClearPropertyFlag {
    public static void main(String[] args) {
        System.setProperty("feature.beta", "true");
        System.out.println("Beta on: " + Boolean.getBoolean("feature.beta"));
        System.out.println("Retries: " + Integer.getInteger("feature.retries", 3));

        System.setProperty("feature.retries", "5");
        System.out.println("Retries: " + Integer.getInteger("feature.retries", 3));

        System.clearProperty("feature.beta");
        System.clearProperty("feature.retries");
        System.out.println("Beta on: " + Boolean.getBoolean("feature.beta"));
        System.out.println("Retries: " + Integer.getInteger("feature.retries", 3));
    }
}

How It Works

  1. Flags read from properties. Boolean.getBoolean("feature.beta") is true because the property is "true"; Integer.getInteger uses the fallback 3 while "feature.retries" is missing.
  2. Parsed for you. After setting "feature.retries" to "5", Integer.getInteger parses the text and returns 5.
  3. Back to defaults. With both keys cleared, the flag reads false and the retry count falls back to 3, the same as before anything was set.

Example 3 — Clean Up Every Key a Test Created

Remember the keys you set, then clear them all at the end and count how many were really removed.

Java
public class SystemClearPropertyCleanup {
    public static void main(String[] args) {
        String[] keys = {"test.user", "test.region", "test.limit"};
        String[] values = {"demo", "eu", "10"};
        for (int i = 0; i < keys.length; i++) {
            System.setProperty(keys[i], values[i]);
        }

        int removed = 0;
        for (String key : keys) {
            if (System.clearProperty(key) != null) {
                removed++;
            }
        }
        System.out.println("Removed: " + removed);
        System.out.println("test.region now: " + System.getProperty("test.region"));

        try {
            System.clearProperty("");
        } catch (IllegalArgumentException e) {
            System.out.println("Empty key: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Parallel arrays. The first loop sets three keys from keys to the matching entries of values.
  2. Count real removals. Each clearProperty returns the old value; all three were set, so none returns null and the count is 3.
  3. Keys must not be empty. clearProperty("") is rejected with IllegalArgumentException, just like getProperty("") and setProperty("", v).

Frequently Asked Questions

The value the property had before it was removed, or null if the property was not set.
clearProperty removes the key, so getProperty returns null. setProperty with an empty string keeps the key with an empty value, so getProperty returns "".
Java 5 (JDK 1.5). Before that, code used System.getProperties().remove(key).
No. It just returns null. It only throws for a null key (NullPointerException) or an empty key (IllegalArgumentException).
No. Other code, including the JDK, may expect them. Only clear properties your own program created.

Next: console()

Get the Console of the interactive terminal, or null when there is none.

console() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful