<jupyter_start><jupyter_text>New to Plotly?Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).You can set up Plotly to work in [online](https://plot.ly/python/getting-started/initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/start-plotting-online).We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!  Imports and CredentialsIn additional to importing python's `requests` and `json` packages, this tutorial also uses [Plotly's REST API](https://api.plot.ly/v2/)First define YOUR [username and api key](https://plot.ly/settings/api) and create `auth` and `headers` to use with `requests`<jupyter_code>import plotly
import plotly.plotly as py

import json
import requests
from requests.auth import HTTPBasicAuth

username = 'private_plotly' # Replace with YOUR USERNAME
api_key = '<KEY>' # Replace with YOUR API KEY

auth = HTTPBasicAuth(username, api_key)
headers = {'Plotly-Client-Platform': 'python'}

plotly.tools.set_credentials_file(username=username, api_key=api_key)<jupyter_output><empty_output><jupyter_text>[Trash](https://api.plot.ly/v2/files/trash) and [Restore](https://api.plot.ly/v2/files/restore)Create a plot and return the url to see the file id which will be used to delete the plot.<jupyter_code>url = py.plot({"data": [{"x": [1, 2, 3],
                         "y": [4, 2, 4]}],
               "layout": {"title": "Let's Trash This Plot<br>(then restore it)"}},
              filename='trash example') 

url<jupyter_output><empty_output><jupyter_text>Include the file id in your request. The file id is your `username:plot_id`<jupyter_code>fid = username+':18'
fid<jupyter_output><empty_output><jupyter_text>The following request moves the plot from the [organize folder](https://plot.ly/organize/home) into the trash. Note: a successful trash request will return a `Response [200]`.<jupyter_code>requests.post('https://api.plot.ly/v2/files/'+fid+'/trash', auth=auth, headers=headers)<jupyter_output><empty_output><jupyter_text>Now if you visit the url, the plot won't be there. However, at this point, there is the option to restore the plot (i.e. move it out of trash and back to the organize folder) with the following request:    [PERMANENT Delete](https://api.plot.ly/v2/files/permanent_delete)This request CANNOT!!!!!!! be restored. Only use `permanent_delete` when absolutely sure the plot is no longer needed.<jupyter_code>url = py.plot({"data": [{"x": [1, 2, 3],
                         "y": [3, 2, 1]}],
               "layout": {"title": "Let's Delete This Plot<br><b>FOREVER!!!!</b>"}},
              filename='PERMANENT delete ex') 
url
fid_permanent_delete = username+':79'
fid_permanent_delete<jupyter_output><empty_output><jupyter_text>To PERMANENTLY delete a plot, first move the plot to the trash (as seen above):<jupyter_code>requests.post('https://api.plot.ly/v2/files/'+fid_permanent_delete+'/trash', auth=auth, headers=headers)<jupyter_output><empty_output><jupyter_text>Then [permanent delete](https://api.plot.ly/v2/files/permanent_delete).Note: a successful permanent delete request will return a `Response [204]` (No Content).<jupyter_code>requests.delete('https://api.plot.ly/v2/files/'+fid_permanent_delete+'/permanent_delete', auth=auth, headers=headers)<jupyter_output><empty_output><jupyter_text>Delete All Plots and Grids PERMANENTLY!In order to delete all plots and grids permanently, you need to delete all of your plots first, then delete all the associated grids.<jupyter_code>def get_pages(username, page_size):
    url = 'https://api.plot.ly/v2/folders/all?user='+username+'&page_size='+str(page_size)
    response = requests.get(url, auth=auth, headers=headers)
    if response.status_code != 200:
        return
    page = json.loads(response.content)
    yield page
    while True:
        resource = page['children']['next'] 
        if not resource:
            break
        response = requests.get(resource, auth=auth, headers=headers)
        if response.status_code != 200:
            break
        page = json.loads(response.content)
        yield page
        
def permanently_delete_files(username, page_size=500, filetype_to_delete='plot'):
    for page in get_pages(username, page_size):
        for x in range(0, len(page['children']['results'])):
            fid = page['children']['results'][x]['fid']
            res = requests.get('https://api.plot.ly/v2/files/' + fid, auth=auth, headers=headers)
            res.raise_for_status()
            if res.status_code == 200:
                json_res = json.loads(res.content)
                if json_res['filetype'] == filetype_to_delete:
                    # move to trash
                    requests.post('https://api.plot.ly/v2/files/'+fid+'/trash', auth=auth, headers=headers)
                    # permanently delete
                    requests.delete('https://api.plot.ly/v2/files/'+fid+'/permanent_delete', auth=auth, headers=headers)

permanently_delete_files(username, filetype_to_delete='plot')
permanently_delete_files(username, filetype_to_delete='grid')
from IPython.display import display, HTML

display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))

#!pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
    'delete.ipynb', 'python/delete-plots/', 'Deleting Plots with the Python API',
    'How to delete plotly graphs in python.',
    name = 'Deleting Plots', language='python', 
    has_thumbnail='true', thumbnail= 'thumbnail/delete.jpg',
    display_as='chart_studio', order=9)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13764456751913992, 'fraction_numerical': 0.010750936634631048, 'mean_word_length': 4.385964912280702, 'pattern_counts': {'":': 10, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 21, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '83409', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1919, 'n_words': 456, 'path': '_posts/python-v3/chart-studio/delete/delete.ipynb', 'repo_name': 'bmb804/documentation', 'size': '6144'}